View source
<?php
namespace Drupal\Tests\field\Functional\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\views\Views;
class FieldUITest extends FieldTestBase {
public static $testViews = [
'test_view_fieldapi',
];
protected static $modules = [
'views_ui',
];
protected $defaultTheme = 'stark';
protected $account;
protected function setUp($import_test_views = TRUE, $modules = [
'field_test_views',
]) : void {
parent::setUp($import_test_views, $modules);
$this->account = $this
->drupalCreateUser([
'administer views',
]);
$this
->drupalLogin($this->account);
$this
->setUpFieldStorages(1, 'text');
$this
->setUpFields();
}
public function testHandlerUI() {
$url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
$this
->drupalGet($url);
$options = $this
->assertSession()
->selectExists('edit-options-type')
->findAll('css', 'option');
$options = array_map(function ($item) {
return $item
->getValue();
}, $options);
$this
->assertEqualsCanonicalizing([
'text_default',
'text_trimmed',
], $options);
$this
->submitForm([
'options[type]' => 'text_trimmed',
], 'Apply');
$this
->drupalGet($url);
$this
->assertTrue($this
->assertSession()
->optionExists('edit-options-type', 'text_trimmed')
->isSelected());
$random_number = rand(100, 400);
$this
->submitForm([
'options[settings][trim_length]' => $random_number,
], 'Apply');
$this
->drupalGet($url);
$this
->assertSession()
->fieldValueEquals('options[settings][trim_length]', $random_number);
$this
->drupalGet('admin/structure/views/view/test_view_fieldapi');
$this
->submitForm([], 'Save');
$view = Views::getView('test_view_fieldapi');
$view
->initHandlers();
$this
->assertEquals('text_trimmed', $view->field['field_name_0']->options['type']);
$this
->assertEquals($random_number, $view->field['field_name_0']->options['settings']['trim_length']);
$edit['options[type]'] = 'text_default';
$this
->drupalGet('admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0');
$this
->submitForm($edit, 'Apply');
$this
->drupalGet('admin/structure/views/view/test_view_fieldapi');
$this
->submitForm([], 'Save');
$view = Views::getView('test_view_fieldapi');
$view
->initHandlers();
$this
->assertEquals('text_default', $view->field['field_name_0']->options['type']);
$this
->assertEquals([], $view->field['field_name_0']->options['settings']);
$dependencies = \Drupal::service('config.manager')
->findConfigEntityDependencies('config', [
$this->fieldStorages[0]
->getConfigDependencyName(),
]);
$this
->assertTrue(isset($dependencies['views.view.test_view_fieldapi']), 'The view is dependent on the field storage.');
}
public function testHandlerUIAggregation() {
$edit = [
'group_by' => '1',
];
$this
->drupalGet('admin/structure/views/nojs/display/test_view_fieldapi/default/group_by');
$this
->submitForm($edit, 'Apply');
$url = "admin/structure/views/nojs/handler/test_view_fieldapi/default/field/field_name_0";
$this
->drupalGet($url);
$this
->assertSession()
->statusCodeEquals(200);
$options = $this
->assertSession()
->selectExists('edit-options-click-sort-column')
->findAll('css', 'option');
$options = array_map(function ($item) {
return $item
->getValue();
}, $options);
$this
->assertEqualsCanonicalizing([
'format',
'value',
], $options);
}
public function testBooleanFilterHandler() {
$field_name = 'field_boolean';
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'node',
'type' => 'boolean',
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'page',
]);
$field
->save();
$url = "admin/structure/views/nojs/add-handler/test_view_fieldapi/default/filter";
$this
->drupalGet($url);
$this
->submitForm([
'name[node__' . $field_name . '.' . $field_name . '_value]' => TRUE,
], 'Add and configure filter criteria');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->elementTextEquals('xpath', '//label[@for="edit-options-value-1"]', 'True');
$this
->assertSession()
->elementTextEquals('xpath', '//label[@for="edit-options-value-0"]', 'False');
$this
->submitForm([], 'Expose filter');
$this
->assertSession()
->elementTextEquals('xpath', '//label[@for="edit-options-value-all"]', '- Any -');
$this
->submitForm([
'options[value]' => 'All',
'options[expose][required]' => FALSE,
], 'Apply');
$this
->submitForm([], 'Save');
$this
->drupalGet('/admin/structure/views/nojs/handler/test_view_fieldapi/default/filter/field_boolean_value');
$this
->assertSession()
->checkboxChecked('edit-options-value-all');
}
}