View source
<?php
namespace Drupal\field\Tests\Boolean;
use Drupal\Component\Utility\Unicode;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\simpletest\WebTestBase;
class BooleanFormatterSettingsTest extends WebTestBase {
public static $modules = [
'field',
'field_ui',
'text',
'node',
'user',
];
protected $bundle;
protected $fieldName;
protected function setUp() {
parent::setUp();
$type_name = Unicode::strtolower($this
->randomMachineName(8)) . '_test';
$type = $this
->drupalCreateContentType(array(
'name' => $type_name,
'type' => $type_name,
));
$this->bundle = $type
->id();
$admin_user = $this
->drupalCreateUser(array(
'access content',
'administer content types',
'administer node fields',
'administer node display',
'bypass node access',
'administer nodes',
));
$this
->drupalLogin($admin_user);
$this->fieldName = Unicode::strtolower($this
->randomMachineName(8));
$field_storage = FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'node',
'type' => 'boolean',
]);
$field_storage
->save();
$instance = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->bundle,
'label' => $this
->randomMachineName(),
]);
$instance
->save();
$display = entity_get_display('node', $this->bundle, 'default')
->setComponent($this->fieldName, [
'type' => 'boolean',
'settings' => [],
]);
$display
->save();
}
function testBooleanFormatterSettings() {
$options = array(
'Yes / No',
'True / False',
'On / Off',
'Enabled / Disabled',
'1 / 0',
'Custom',
);
$default = 'Field settings (@on / @off)';
$settings = array(
array(
'Yes',
'No',
),
array(
'On',
'Off',
),
array(
'TRUE',
'FALSE',
),
);
foreach ($settings as $values) {
$this
->drupalGet('admin/structure/types/manage/' . $this->bundle . '/fields/node.' . $this->bundle . '.' . $this->fieldName);
$this
->drupalPostForm(NULL, array(
'settings[on_label]' => $values[0],
'settings[off_label]' => $values[1],
), 'Save settings');
$this
->drupalGet('admin/structure/types/manage/' . $this->bundle . '/display');
$this
->drupalPostAjaxForm(NULL, array(), $this->fieldName . '_settings_edit');
foreach ($options as $string) {
$this
->assertText($string);
}
$this
->assertText(SafeMarkup::format($default, array(
'@on' => $values[0],
'@off' => $values[1],
)));
}
}
}