annotate core/modules/field/src/Tests/Views/FieldUITest.php @ 6:875880e46745

Styling
author Chris Cannam
date Fri, 08 Dec 2017 13:21:27 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\field\Tests\Views;
Chris@0 4
Chris@0 5 use Drupal\field\Entity\FieldConfig;
Chris@0 6 use Drupal\field\Entity\FieldStorageConfig;
Chris@0 7 use Drupal\views\Views;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests the UI of the field field handler.
Chris@0 11 *
Chris@0 12 * @group field
Chris@0 13 * @see \Drupal\field\Plugin\views\field\Field
Chris@0 14 */
Chris@0 15 class FieldUITest extends FieldTestBase {
Chris@0 16
Chris@0 17 /**
Chris@0 18 * Views used by this test.
Chris@0 19 *
Chris@0 20 * @var array
Chris@0 21 */
Chris@0 22 public static $testViews = ['test_view_fieldapi'];
Chris@0 23
Chris@0 24 /**
Chris@0 25 * Modules to enable.
Chris@0 26 *
Chris@0 27 * @var array
Chris@0 28 */
Chris@0 29 public static $modules = ['views_ui'];
Chris@0 30
Chris@0 31 /**
Chris@0 32 * A user with the 'administer views' permission.
Chris@0 33 *
Chris@0 34 * @var \Drupal\user\UserInterface
Chris@0 35 */
Chris@0 36 protected $account;
Chris@0 37
Chris@0 38 /**
Chris@0 39 * {@inheritdoc}
Chris@0 40 */
Chris@0 41 protected function setUp() {
Chris@0 42 parent::setUp();
Chris@0 43
Chris@0 44 $this->account = $this->drupalCreateUser(['administer views']);
Chris@0 45 $this->drupalLogin($this->account);
Chris@0 46
Chris@0 47 $this->setUpFieldStorages(1, 'text');
Chris@0 48 $this->setUpFields();
Chris@0 49 }
Chris@0 50
Chris@0 51 /**
Chris@0 52 * Tests basic field handler settings in the UI.
Chris@0 53 */
Chris@0 54 public function testHandlerUI() {
Chris@0 55 $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
Chris@0 56 $this->drupalGet($url);
Chris@0 57
Chris@0 58 // Tests the available formatter options.
Chris@0 59 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-type']);
Chris@0 60 $options = array_map(function ($item) {
Chris@0 61 return (string) $item->attributes()->value[0];
Chris@0 62 }, $result);
Chris@0 63 // @todo Replace this sort by assertArray once it's in.
Chris@0 64 sort($options, SORT_STRING);
Chris@0 65 $this->assertEqual($options, ['text_default', 'text_trimmed'], 'The text formatters for a simple text field appear as expected.');
Chris@0 66
Chris@0 67 $this->drupalPostForm(NULL, ['options[type]' => 'text_trimmed'], t('Apply'));
Chris@0 68
Chris@0 69 $this->drupalGet($url);
Chris@0 70 $this->assertOptionSelected('edit-options-type', 'text_trimmed');
Chris@0 71
Chris@0 72 $random_number = rand(100, 400);
Chris@0 73 $this->drupalPostForm(NULL, ['options[settings][trim_length]' => $random_number], t('Apply'));
Chris@0 74 $this->drupalGet($url);
Chris@0 75 $this->assertFieldByName('options[settings][trim_length]', $random_number, 'The formatter setting got saved.');
Chris@0 76
Chris@0 77 // Save the view and test whether the settings are saved.
Chris@0 78 $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
Chris@0 79 $view = Views::getView('test_view_fieldapi');
Chris@0 80 $view->initHandlers();
Chris@0 81 $this->assertEqual($view->field['field_name_0']->options['type'], 'text_trimmed');
Chris@0 82 $this->assertEqual($view->field['field_name_0']->options['settings']['trim_length'], $random_number);
Chris@0 83
Chris@0 84 // Now change the formatter back to 'default' which doesn't have any
Chris@0 85 // settings. We want to ensure that the settings are empty then.
Chris@0 86 $edit['options[type]'] = 'text_default';
Chris@0 87 $this->drupalPostForm('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0', $edit, t('Apply'));
Chris@0 88 $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save'));
Chris@0 89 $view = Views::getView('test_view_fieldapi');
Chris@0 90 $view->initHandlers();
Chris@0 91 $this->assertEqual($view->field['field_name_0']->options['type'], 'text_default');
Chris@0 92 $this->assertEqual($view->field['field_name_0']->options['settings'], []);
Chris@0 93
Chris@0 94 // Ensure that the view depends on the field storage.
Chris@0 95 $dependencies = \Drupal::service('config.manager')->findConfigEntityDependents('config', [$this->fieldStorages[0]->getConfigDependencyName()]);
Chris@0 96 $this->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.');
Chris@0 97 }
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Tests the basic field handler form when aggregation is enabled.
Chris@0 101 */
Chris@0 102 public function testHandlerUIAggregation() {
Chris@0 103 // Enable aggregation.
Chris@0 104 $edit = ['group_by' => '1'];
Chris@0 105 $this->drupalPostForm('admin/structure/views/nojs/display/test_view_fieldapi/default/group_by', $edit, t('Apply'));
Chris@0 106
Chris@0 107 $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
Chris@0 108 $this->drupalGet($url);
Chris@0 109 $this->assertResponse(200);
Chris@0 110
Chris@0 111 // Test the click sort column options.
Chris@0 112 // Tests the available formatter options.
Chris@0 113 $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-click-sort-column']);
Chris@0 114 $options = array_map(function ($item) {
Chris@0 115 return (string) $item->attributes()->value[0];
Chris@0 116 }, $result);
Chris@0 117 sort($options, SORT_STRING);
Chris@0 118
Chris@0 119 $this->assertEqual($options, ['format', 'value'], 'The expected sort field options were found.');
Chris@0 120 }
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Tests adding a boolean field filter handler.
Chris@0 124 */
Chris@0 125 public function testBooleanFilterHandler() {
Chris@0 126 // Create a boolean field.
Chris@0 127 $field_name = 'field_boolean';
Chris@0 128 $field_storage = FieldStorageConfig::create([
Chris@0 129 'field_name' => $field_name,
Chris@0 130 'entity_type' => 'node',
Chris@0 131 'type' => 'boolean',
Chris@0 132 ]);
Chris@0 133 $field_storage->save();
Chris@0 134 $field = FieldConfig::create([
Chris@0 135 'field_storage' => $field_storage,
Chris@0 136 'bundle' => 'page',
Chris@0 137 ]);
Chris@0 138 $field->save();
Chris@0 139
Chris@0 140 $url = "admin/structure/views/nojs/add-handler/test_view_fieldapi/default/filter";
Chris@0 141 $this->drupalPostForm($url, ['name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE], t('Add and configure @handler', ['@handler' => t('filter criteria')]));
Chris@0 142 $this->assertResponse(200);
Chris@0 143 // Verify that using a boolean field as a filter also results in using the
Chris@0 144 // boolean plugin.
Chris@0 145 $option = $this->xpath('//label[@for="edit-options-value-1"]');
Chris@0 146 $this->assertEqual(t('True'), (string) $option[0]);
Chris@0 147 $option = $this->xpath('//label[@for="edit-options-value-0"]');
Chris@0 148 $this->assertEqual(t('False'), (string) $option[0]);
Chris@0 149
Chris@0 150 // Expose the filter and see if the 'Any' option is added and if we can save
Chris@0 151 // it.
Chris@0 152 $this->drupalPostForm(NULL, [], 'Expose filter');
Chris@0 153 $option = $this->xpath('//label[@for="edit-options-value-all"]');
Chris@0 154 $this->assertEqual(t('- Any -'), (string) $option[0]);
Chris@0 155 $this->drupalPostForm(NULL, ['options[value]' => 'All', 'options[expose][required]' => FALSE], 'Apply');
Chris@0 156 $this->drupalPostForm(NULL, [], 'Save');
Chris@0 157 $this->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value');
Chris@0 158 $this->assertFieldChecked('edit-options-value-all');
Chris@0 159 }
Chris@0 160
Chris@0 161 }