Chris@0: account = $this->drupalCreateUser(['administer views']); Chris@0: $this->drupalLogin($this->account); Chris@0: Chris@0: $this->setUpFieldStorages(1, 'text'); Chris@0: $this->setUpFields(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests basic field handler settings in the UI. Chris@0: */ Chris@0: public function testHandlerUI() { Chris@0: $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0"; Chris@0: $this->drupalGet($url); Chris@0: Chris@0: // Tests the available formatter options. Chris@0: $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-type']); Chris@0: $options = array_map(function ($item) { Chris@0: return (string) $item->attributes()->value[0]; Chris@0: }, $result); Chris@0: // @todo Replace this sort by assertArray once it's in. Chris@0: sort($options, SORT_STRING); Chris@0: $this->assertEqual($options, ['text_default', 'text_trimmed'], 'The text formatters for a simple text field appear as expected.'); Chris@0: Chris@0: $this->drupalPostForm(NULL, ['options[type]' => 'text_trimmed'], t('Apply')); Chris@0: Chris@0: $this->drupalGet($url); Chris@0: $this->assertOptionSelected('edit-options-type', 'text_trimmed'); Chris@0: Chris@0: $random_number = rand(100, 400); Chris@0: $this->drupalPostForm(NULL, ['options[settings][trim_length]' => $random_number], t('Apply')); Chris@0: $this->drupalGet($url); Chris@0: $this->assertFieldByName('options[settings][trim_length]', $random_number, 'The formatter setting got saved.'); Chris@0: Chris@0: // Save the view and test whether the settings are saved. Chris@0: $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save')); Chris@0: $view = Views::getView('test_view_fieldapi'); Chris@0: $view->initHandlers(); Chris@0: $this->assertEqual($view->field['field_name_0']->options['type'], 'text_trimmed'); Chris@0: $this->assertEqual($view->field['field_name_0']->options['settings']['trim_length'], $random_number); Chris@0: Chris@0: // Now change the formatter back to 'default' which doesn't have any Chris@0: // settings. We want to ensure that the settings are empty then. Chris@0: $edit['options[type]'] = 'text_default'; Chris@0: $this->drupalPostForm('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0', $edit, t('Apply')); Chris@0: $this->drupalPostForm('admin/structure/views/view/test_view_fieldapi', [], t('Save')); Chris@0: $view = Views::getView('test_view_fieldapi'); Chris@0: $view->initHandlers(); Chris@0: $this->assertEqual($view->field['field_name_0']->options['type'], 'text_default'); Chris@0: $this->assertEqual($view->field['field_name_0']->options['settings'], []); Chris@0: Chris@0: // Ensure that the view depends on the field storage. Chris@0: $dependencies = \Drupal::service('config.manager')->findConfigEntityDependents('config', [$this->fieldStorages[0]->getConfigDependencyName()]); Chris@0: $this->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests the basic field handler form when aggregation is enabled. Chris@0: */ Chris@0: public function testHandlerUIAggregation() { Chris@0: // Enable aggregation. Chris@0: $edit = ['group_by' => '1']; Chris@0: $this->drupalPostForm('admin/structure/views/nojs/display/test_view_fieldapi/default/group_by', $edit, t('Apply')); Chris@0: Chris@0: $url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0"; Chris@0: $this->drupalGet($url); Chris@0: $this->assertResponse(200); Chris@0: Chris@0: // Test the click sort column options. Chris@0: // Tests the available formatter options. Chris@0: $result = $this->xpath('//select[@id=:id]/option', [':id' => 'edit-options-click-sort-column']); Chris@0: $options = array_map(function ($item) { Chris@0: return (string) $item->attributes()->value[0]; Chris@0: }, $result); Chris@0: sort($options, SORT_STRING); Chris@0: Chris@0: $this->assertEqual($options, ['format', 'value'], 'The expected sort field options were found.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests adding a boolean field filter handler. Chris@0: */ Chris@0: public function testBooleanFilterHandler() { Chris@0: // Create a boolean field. Chris@0: $field_name = 'field_boolean'; Chris@0: $field_storage = FieldStorageConfig::create([ Chris@0: 'field_name' => $field_name, Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'boolean', Chris@0: ]); Chris@0: $field_storage->save(); Chris@0: $field = FieldConfig::create([ Chris@0: 'field_storage' => $field_storage, Chris@0: 'bundle' => 'page', Chris@0: ]); Chris@0: $field->save(); Chris@0: Chris@0: $url = "admin/structure/views/nojs/add-handler/test_view_fieldapi/default/filter"; Chris@0: $this->drupalPostForm($url, ['name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE], t('Add and configure @handler', ['@handler' => t('filter criteria')])); Chris@0: $this->assertResponse(200); Chris@0: // Verify that using a boolean field as a filter also results in using the Chris@0: // boolean plugin. Chris@0: $option = $this->xpath('//label[@for="edit-options-value-1"]'); Chris@0: $this->assertEqual(t('True'), (string) $option[0]); Chris@0: $option = $this->xpath('//label[@for="edit-options-value-0"]'); Chris@0: $this->assertEqual(t('False'), (string) $option[0]); Chris@0: Chris@0: // Expose the filter and see if the 'Any' option is added and if we can save Chris@0: // it. Chris@0: $this->drupalPostForm(NULL, [], 'Expose filter'); Chris@0: $option = $this->xpath('//label[@for="edit-options-value-all"]'); Chris@0: $this->assertEqual(t('- Any -'), (string) $option[0]); Chris@0: $this->drupalPostForm(NULL, ['options[value]' => 'All', 'options[expose][required]' => FALSE], 'Apply'); Chris@0: $this->drupalPostForm(NULL, [], 'Save'); Chris@0: $this->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value'); Chris@0: $this->assertFieldChecked('edit-options-value-all'); Chris@0: } Chris@0: Chris@0: }