Newer
Older
Damian Lee
committed
<?php
/**
* @file
* Definition of Drupal\views\Tests\Wizard\TaggedWithTest.
Damian Lee
committed
*/
namespace Drupal\views\Tests\Wizard;
Damian Lee
committed
use Drupal\Core\Language\Language;
Damian Lee
committed
/**
* Tests the ability of the views wizard to create views filtered by taxonomy.
*/
class TaggedWithTest extends WizardTestBase {
Tim Plunkett
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('taxonomy');
Damian Lee
committed
protected $node_type_with_tags;
Damian Lee
committed
protected $node_type_without_tags;
Damian Lee
committed
protected $tag_vocabulary;
Damian Lee
committed
protected $tag_field;
Damian Lee
committed
protected $tag_instance;
public static function getInfo() {
return array(
'name' => 'Taxonomy functionality',
Damian Lee
committed
'description' => 'Test the ability of the views wizard to create views filtered by taxonomy.',
'group' => 'Views Wizard',
Damian Lee
committed
);
}
function setUp() {
parent::setUp();
// Create two content types. One will have an autocomplete tagging field,
// and one won't.
$this->node_type_with_tags = $this->drupalCreateContentType();
$this->node_type_without_tags = $this->drupalCreateContentType();
// Create the vocabulary for the tag field.
$this->tag_vocabulary = entity_create('taxonomy_vocabulary', array(
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
Damian Lee
committed
));
$this->tag_vocabulary->save();
// Create the tag field itself.
$this->tag_field = array(
'field_name' => 'field_views_testing_tags',
'type' => 'taxonomy_term_reference',
'cardinality' => FIELD_CARDINALITY_UNLIMITED,
'settings' => array(
'allowed_values' => array(
array(
'vocabulary' => $this->tag_vocabulary->id(),
Damian Lee
committed
'parent' => 0,
),
),
),
);
field_create_field($this->tag_field);
// Create an instance of the tag field on one of the content types, and
// configure it to display an autocomplete widget.
$this->tag_instance = array(
'field_name' => 'field_views_testing_tags',
'entity_type' => 'node',
'bundle' => $this->node_type_with_tags->type,
);
field_create_instance($this->tag_instance);
Alex Pott
committed
entity_get_form_display('node', $this->node_type_with_tags->type, 'default')
->setComponent('field_views_testing_tags', array(
'type' => 'taxonomy_autocomplete',
))
->save();
entity_get_display('node', $this->node_type_with_tags->type, 'default')
->setComponent('field_views_testing_tags', array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
))
->save();
entity_get_display('node', $this->node_type_with_tags->type, 'teaser')
->setComponent('field_views_testing_tags', array(
'type' => 'taxonomy_term_reference_link',
'weight' => 10,
))
->save();
Damian Lee
committed
}
/**
* Tests the "tagged with" functionality.
*/
function testTaggedWith() {
// In this test we will only create nodes that have an instance of the tag
// field.
$node_add_path = 'node/add/' . $this->node_type_with_tags->type;
// Create three nodes, with different tags.
$tag_field = $this->tag_field['field_name'] . '[' . Language::LANGCODE_NOT_SPECIFIED . ']';
Damian Lee
committed
$edit = array();
$edit['title'] = $node_tag1_title = $this->randomName();
$edit[$tag_field] = 'tag1';
$this->drupalPost($node_add_path, $edit, t('Save'));
$edit = array();
$edit['title'] = $node_tag1_tag2_title = $this->randomName();
$edit[$tag_field] = 'tag1, tag2';
$this->drupalPost($node_add_path, $edit, t('Save'));
$edit = array();
$edit['title'] = $node_no_tags_title = $this->randomName();
$this->drupalPost($node_add_path, $edit, t('Save'));
// Create a view that filters by taxonomy term "tag1". It should show only
// the two nodes from above that are tagged with "tag1".
$view1 = array();
// First select the node type and update the form so the correct tag field
// is used.
$view1['show[type]'] = $this->node_type_with_tags->type;
$this->drupalPost('admin/structure/views/add', $view1, t('Update "of type" choice'));
// Now resubmit the entire form to the same URL.
Angie Byron
committed
$view1['label'] = $this->randomName(16);
Angie Byron
committed
$view1['id'] = strtolower($this->randomName(16));
Damian Lee
committed
$view1['description'] = $this->randomName(16);
$view1['show[tagged_with]'] = 'tag1';
$view1['page[create]'] = 1;
$view1['page[title]'] = $this->randomName(16);
$view1['page[path]'] = $this->randomName(16);
Dries Buytaert
committed
$this->drupalPost(NULL, $view1, t('Save and edit'));
Damian Lee
committed
// Visit the page and check that the nodes we expect are present and the
// ones we don't expect are absent.
$this->drupalGet($view1['page[path]']);
Dries Buytaert
committed
$this->assertResponse(200);
Damian Lee
committed
$this->assertText($node_tag1_title);
$this->assertText($node_tag1_tag2_title);
$this->assertNoText($node_no_tags_title);
// Create a view that filters by taxonomy term "tag2". It should show only
// the one node from above that is tagged with "tag2".
$view2 = array();
$view2['show[type]'] = $this->node_type_with_tags->type;
$this->drupalPost('admin/structure/views/add', $view2, t('Update "of type" choice'));
Tim Plunkett
committed
$this->assertResponse(200);
Angie Byron
committed
$view2['label'] = $this->randomName(16);
Angie Byron
committed
$view2['id'] = strtolower($this->randomName(16));
Damian Lee
committed
$view2['description'] = $this->randomName(16);
$view2['show[tagged_with]'] = 'tag2';
$view2['page[create]'] = 1;
$view2['page[title]'] = $this->randomName(16);
$view2['page[path]'] = $this->randomName(16);
Dries Buytaert
committed
$this->drupalPost(NULL, $view2, t('Save and edit'));
Tim Plunkett
committed
$this->assertResponse(200);
Damian Lee
committed
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
$this->drupalGet($view2['page[path]']);
$this->assertNoText($node_tag1_title);
$this->assertText($node_tag1_tag2_title);
$this->assertNoText($node_no_tags_title);
}
/**
* Tests that the "tagged with" form element only shows for node types that support it.
*/
function testTaggedWithByNodeType() {
// The tagging field is associated with one of our node types only. So the
// "tagged with" form element on the view wizard should appear on the form
// by default (when the wizard is configured to display all content) and
// also when the node type that has the tagging field is selected, but not
// when the node type that doesn't have the tagging field is selected.
$tags_xpath = '//input[@name="show[tagged_with]"]';
$this->drupalGet('admin/structure/views/add');
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->node_type_with_tags->type;
$this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->node_type_without_tags->type;
$this->drupalPost(NULL, $view, t('Update "of type" choice'));
$this->assertNoFieldByXpath($tags_xpath);
// If we add an instance of the tagging field to the second node type, the
// "tagged with" form element should not appear for it too.
$instance = $this->tag_instance;
$instance['bundle'] = $this->node_type_without_tags->type;
field_create_instance($instance);
Alex Pott
committed
entity_get_form_display('node', $this->node_type_without_tags->type, 'default')
->setComponent('field_views_testing_tags', array(
'type' => 'taxonomy_autocomplete',
))
->save();
Damian Lee
committed
$view['show[type]'] = $this->node_type_with_tags->type;
$this->drupalPost('admin/structure/views/add', $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
$view['show[type]'] = $this->node_type_without_tags->type;
$this->drupalPost(NULL, $view, t('Update "of type" choice'));
$this->assertFieldByXpath($tags_xpath);
}