View source
<?php
namespace Drupal\views\Tests\Wizard;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
class TaggedWithTest extends WizardTestBase {
use EntityReferenceTestTrait;
public static $modules = array(
'taxonomy',
);
protected $nodeTypeWithTags;
protected $nodeTypeWithoutTags;
protected $tagVocabulary;
protected $tagFieldStorage;
protected $tagFieldName;
protected $tagField;
protected function setUp() {
parent::setUp();
$this->nodeTypeWithTags = $this
->drupalCreateContentType();
$this->nodeTypeWithoutTags = $this
->drupalCreateContentType();
$this->tagVocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
));
$this->tagVocabulary
->save();
$this->tagFieldName = 'field_views_testing_tags';
$handler_settings = array(
'target_bundles' => array(
$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);
entity_get_form_display('node', $this->nodeTypeWithTags
->id(), 'default')
->setComponent($this->tagFieldName, array(
'type' => 'entity_reference_autocomplete_tags',
))
->save();
entity_get_display('node', $this->nodeTypeWithTags
->id(), 'default')
->setComponent($this->tagFieldName, array(
'type' => 'entity_reference_label',
'weight' => 10,
))
->save();
entity_get_display('node', $this->nodeTypeWithTags
->id(), 'teaser')
->setComponent('field_views_testing_tags', array(
'type' => 'entity_reference_label',
'weight' => 10,
))
->save();
}
function testTaggedWith() {
$node_add_path = 'node/add/' . $this->nodeTypeWithTags
->id();
$edit = array();
$edit['title[0][value]'] = $node_tag1_title = $this
->randomMachineName();
$edit[$this->tagFieldName . '[target_id]'] = 'tag1';
$this
->drupalPostForm($node_add_path, $edit, t('Save'));
$edit = array();
$edit['title[0][value]'] = $node_tag1_tag2_title = $this
->randomMachineName();
$edit[$this->tagFieldName . '[target_id]'] = 'tag1, tag2';
$this
->drupalPostForm($node_add_path, $edit, t('Save'));
$edit = array();
$edit['title[0][value]'] = $node_no_tags_title = $this
->randomMachineName();
$this
->drupalPostForm($node_add_path, $edit, t('Save'));
$view1 = array();
$view1['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalPostForm('admin/structure/views/add', $view1, t('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
->drupalPostForm(NULL, $view1, t('Save and edit'));
$this
->drupalGet($view1['page[path]']);
$this
->assertResponse(200);
$this
->assertText($node_tag1_title);
$this
->assertText($node_tag1_tag2_title);
$this
->assertNoText($node_no_tags_title);
$view2 = array();
$view2['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalPostForm('admin/structure/views/add', $view2, t('Update "of type" choice'));
$this
->assertResponse(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
->drupalPostForm(NULL, $view2, t('Save and edit'));
$this
->assertResponse(200);
$this
->drupalGet($view2['page[path]']);
$this
->assertNoText($node_tag1_title);
$this
->assertText($node_tag1_tag2_title);
$this
->assertNoText($node_no_tags_title);
}
function testTaggedWithByNodeType() {
$tags_xpath = '//input[@name="show[tagged_with]"]';
$this
->drupalGet('admin/structure/views/add');
$this
->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
$this
->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithoutTags
->id();
$this
->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
$this
->assertNoFieldByXpath($tags_xpath);
entity_create('field_config', array(
'field_name' => $this->tagFieldName,
'entity_type' => 'node',
'bundle' => $this->nodeTypeWithoutTags
->id(),
'settings' => array(
'handler' => 'default',
'handler_settings' => array(
'target_bundles' => array(
$this->tagVocabulary
->id() => $this->tagVocabulary
->id(),
),
'auto_create' => TRUE,
),
),
))
->save();
entity_get_form_display('node', $this->nodeTypeWithoutTags
->id(), 'default')
->setComponent($this->tagFieldName, array(
'type' => 'entity_reference_autocomplete_tags',
))
->save();
$view['show[type]'] = $this->nodeTypeWithTags
->id();
$this
->drupalPostForm('admin/structure/views/add', $view, t('Update "of type" choice'));
$this
->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->nodeTypeWithoutTags
->id();
$this
->drupalPostForm(NULL, $view, t('Update "of type" choice (2)'));
$this
->assertFieldByXpath($tags_xpath);
}
}