View source
<?php
namespace Drupal\Tests\views\Functional\Wizard;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
class TaggedWithTest extends WizardTestBase {
use EntityReferenceTestTrait;
protected static $modules = [
'taxonomy',
];
protected $defaultTheme = 'stark';
protected $nodeTypeWithTags;
protected $nodeTypeWithoutTags;
protected $tagVocabulary;
protected $tagFieldStorage;
protected $tagFieldName;
protected $tagField;
protected function setUp($import_test_views = TRUE, $modules = []) : void {
parent::setUp($import_test_views, $modules);
$this->nodeTypeWithTags = $this
->drupalCreateContentType();
$this->nodeTypeWithoutTags = $this
->drupalCreateContentType();
$this->tagVocabulary = Vocabulary::create([
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
]);
$this->tagVocabulary
->save();
$this->tagFieldName = 'field_views_testing_tags';
$handler_settings = [
'target_bundles' => [
$this->tagVocabulary
->id() => $this->tagVocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->createEntityReferenceField('node', $this->nodeTypeWithTags
->id(), $this->tagFieldName, NULL, 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$display_repository = \Drupal::service('entity_display.repository');
$display_repository
->getFormDisplay('node', $this->nodeTypeWithTags
->id())
->setComponent($this->tagFieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
$display_repository
->getViewDisplay('node', $this->nodeTypeWithTags
->id())
->setComponent($this->tagFieldName, [
'type' => 'entity_reference_label',
'weight' => 10,
])
->save();
$display_repository
->getViewDisplay('node', $this->nodeTypeWithTags
->id(), 'teaser')
->setComponent('field_views_testing_tags', [
'type' => 'entity_reference_label',
'weight' => 10,
])
->save();
}
public function testTaggedWith() {
$node_add_path = 'node/add/' . $this->nodeTypeWithTags
->id();
$edit = [];
$edit['title[0][value]'] = $node_tag1_title = $this
->randomMachineName();
$edit[$this->tagFieldName . '[target_id]'] = 'tag1';
$this
->drupalGet($node_add_path);
$this
->submitForm($edit, 'Save');
$edit = [];
$edit['title[0][value]'] = $node_tag1_tag2_title = $this
->randomMachineName();
$edit[$this->tagFieldName . '[target_id]'] = 'tag1, tag2';
$this
->drupalGet($node_add_path);
$this
->submitForm($edit, 'Save');
$edit = [];
$edit['title[0][value]'] = $node_no_tags_title = $this
->randomMachineName();
$this
->drupalGet($node_add_path);
$this
->submitForm($edit, 'Save');
$view1 = [];
$view1['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view1, 'Update "of type" choice');
$view1['label'] = $this
->randomMachineName(16);
$view1['id'] = strtolower($this
->randomMachineName(16));
$view1['description'] = $this
->randomMachineName(16);
$view1['show[tagged_with]'] = 'tag1';
$view1['page[create]'] = 1;
$view1['page[title]'] = $this
->randomMachineName(16);
$view1['page[path]'] = $this
->randomMachineName(16);
$this
->submitForm($view1, 'Save and edit');
$this
->drupalGet($view1['page[path]']);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains($node_tag1_title);
$this
->assertSession()
->pageTextContains($node_tag1_tag2_title);
$this
->assertSession()
->pageTextNotContains($node_no_tags_title);
$view2 = [];
$view2['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view2, 'Update "of type" choice');
$this
->assertSession()
->statusCodeEquals(200);
$view2['label'] = $this
->randomMachineName(16);
$view2['id'] = strtolower($this
->randomMachineName(16));
$view2['description'] = $this
->randomMachineName(16);
$view2['show[tagged_with]'] = 'tag2';
$view2['page[create]'] = 1;
$view2['page[title]'] = $this
->randomMachineName(16);
$view2['page[path]'] = $this
->randomMachineName(16);
$this
->submitForm($view2, 'Save and edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet($view2['page[path]']);
$this
->assertSession()
->pageTextNotContains($node_tag1_title);
$this
->assertSession()
->pageTextContains($node_tag1_tag2_title);
$this
->assertSession()
->pageTextNotContains($node_no_tags_title);
}
public function testTaggedWithByNodeType() {
$this
->drupalGet('admin/structure/views/add');
$this
->assertSession()
->fieldExists("show[tagged_with]");
$view['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view, 'Update "of type" choice');
$this
->assertSession()
->fieldExists("show[tagged_with]");
$view['show[type]'] = $this->nodeTypeWithoutTags
->id();
$this
->submitForm($view, 'Update "of type" choice (2)');
$this
->assertSession()
->fieldNotExists("show[tagged_with]");
FieldConfig::create([
'field_name' => $this->tagFieldName,
'entity_type' => 'node',
'bundle' => $this->nodeTypeWithoutTags
->id(),
'settings' => [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => [
$this->tagVocabulary
->id() => $this->tagVocabulary
->id(),
],
'auto_create' => TRUE,
],
],
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $this->nodeTypeWithoutTags
->id())
->setComponent($this->tagFieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
$view['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view, 'Update "of type" choice');
$this
->assertSession()
->fieldExists("show[tagged_with]");
$view['show[type]'] = $this->nodeTypeWithoutTags
->id();
$this
->submitForm($view, 'Update "of type" choice (2)');
$this
->assertSession()
->fieldExists("show[tagged_with]");
}
public function testTaggedWithByViewReference() {
Term::create([
'name' => 'term1',
'vid' => 'views_testing_tags',
]);
$tags_xpath = '//input[@name="show[tagged_with]"]';
FieldConfig::create([
'field_name' => $this->tagFieldName,
'entity_type' => 'node',
'bundle' => $this->nodeTypeWithoutTags
->id(),
'settings' => [
'handler' => 'views',
'handler_settings' => [],
],
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', $this->nodeTypeWithoutTags
->id())
->setComponent($this->tagFieldName, [
'type' => 'entity_reference_autocomplete_tags',
])
->save();
$view['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalGet('admin/structure/views/add');
$this
->submitForm($view, 'Update "of type" choice');
$this
->assertNotEmpty($this
->xpath($tags_xpath));
$view['show[type]'] = $this->nodeTypeWithoutTags
->id();
$this
->submitForm($view, 'Update "of type" choice (2)');
$this
->assertNotEmpty($this
->xpath($tags_xpath));
$this
->submitForm([
'show[tagged_with]' => 'term1',
], 'Save and edit');
$this
->assertSession()
->statusCodeEquals(200);
$this
->getSession()
->getPage()
->hasContent('Has taxonomy term (= term1)');
}
}