View source
<?php
namespace Drupal\Tests\link_field_autocomplete_filter\Functional;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\BrowserTestBase;
class AutocompleteFilterTest extends BrowserTestBase {
use StringTranslationTrait;
protected $defaultTheme = 'stable';
protected static $modules = [
'node',
'link',
'field_ui',
'link_field_autocomplete_filter',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->drupalCreateContentType([
'type' => 'first',
'name' => 'First',
]);
$this
->drupalCreateContentType([
'type' => 'second',
'name' => 'Second',
]);
$this
->drupalCreateContentType([
'type' => 'third',
'name' => 'Third',
]);
$this->adminUser = $this
->drupalCreateUser([], NULL, TRUE);
}
public function testAutocompleteFilterUi() {
$this
->drupalLogin($this->adminUser);
$type_path = 'admin/structure/types/manage/first/fields/add-field';
$this
->drupalGet($type_path);
$initial_edit = [
'new_storage_type' => 'link',
'label' => 'Link field',
'field_name' => 'link_field',
];
$this
->drupalPostForm($type_path, $initial_edit, $this
->t('Save and continue'));
$this
->getSession()
->getPage()
->pressButton('Save field settings');
$this
->assertSession()
->pageTextContains('Autocomplete Filter');
$this
->assertSession()
->checkboxNotChecked('First');
$this
->assertSession()
->checkboxNotChecked('Second');
$this
->assertSession()
->checkboxNotChecked('Third');
$edit = [
'third_party_settings[link_field_autocomplete_filter][negate]' => 0,
'third_party_settings[link_field_autocomplete_filter][allowed_content_types][second]' => TRUE,
];
$this
->drupalPostForm(NULL, $edit, 'Save settings');
$this
->drupalGet('admin/structure/types/manage/first/fields/node.first.field_link_field');
$this
->assertSession()
->fieldValueEquals('third_party_settings[link_field_autocomplete_filter][negate]', 0);
$this
->assertSession()
->checkboxChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][second]');
$this
->assertSession()
->checkboxNotChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][first]');
$this
->assertSession()
->checkboxNotChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][third]');
$field_config = FieldConfig::load('node.first.field_link_field');
$third_party = $field_config
->getThirdPartySettings('link_field_autocomplete_filter');
$this
->assertEquals([
'negate' => FALSE,
'allowed_content_types' => [
'second' => 'second',
'first' => 0,
'third' => 0,
],
], $third_party);
$nodes = [];
foreach ([
'first',
'second',
'third',
] as $type) {
$node = Node::create([
'type' => $type,
'title' => 'Node of type ' . $type,
]);
$node
->save();
$nodes[$type] = $node;
}
$cases = [
'first' => FALSE,
'second' => TRUE,
'third' => FALSE,
];
foreach ($cases as $node_type => $valid) {
$this
->drupalGet($nodes['first']
->toUrl('edit-form'));
$this
->getSession()
->getPage()
->fillField('URL', sprintf('Node of type %s (%s)', $node_type, $nodes[$node_type]
->id()));
$this
->getSession()
->getPage()
->pressButton('Save');
if (!$valid) {
$this
->assertSession()
->pageTextContains(sprintf('This node: %s (type %s) cannot be referenced.', $nodes[$node_type]
->id(), $node_type));
continue;
}
$this
->assertSession()
->pageTextContains('Node of type first has been updated.');
}
$edit = [
'third_party_settings[link_field_autocomplete_filter][negate]' => 1,
'third_party_settings[link_field_autocomplete_filter][allowed_content_types][second]' => TRUE,
];
$this
->drupalPostForm('admin/structure/types/manage/first/fields/node.first.field_link_field', $edit, 'Save settings');
$this
->drupalGet('admin/structure/types/manage/first/fields/node.first.field_link_field');
$this
->assertSession()
->fieldValueEquals('third_party_settings[link_field_autocomplete_filter][negate]', 1);
$this
->assertSession()
->checkboxChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][second]');
$this
->assertSession()
->checkboxNotChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][first]');
$this
->assertSession()
->checkboxNotChecked('third_party_settings[link_field_autocomplete_filter][allowed_content_types][third]');
$cases = [
'first' => TRUE,
'second' => FALSE,
'third' => TRUE,
];
foreach ($cases as $node_type => $valid) {
$this
->drupalGet($nodes['first']
->toUrl('edit-form'));
$this
->getSession()
->getPage()
->fillField('URL', sprintf('Node of type %s (%s)', $node_type, $nodes[$node_type]
->id()));
$this
->getSession()
->getPage()
->pressButton('Save');
if (!$valid) {
$this
->assertSession()
->pageTextContains(sprintf('This node: %s (type %s) cannot be referenced.', $nodes[$node_type]
->id(), $node_type));
continue;
}
$this
->assertSession()
->pageTextContains('Node of type first has been updated.');
}
}
}