Commit dc4adc57 authored by alexpott's avatar alexpott

Issue #2887134 by Mile23, nlisgo, Lendude, naveenvalecha: Convert web tests to...

Issue #2887134 by Mile23, nlisgo, Lendude, naveenvalecha: Convert web tests to browser tests for taxonomy module Part -2
parent d122ac7c
<?php <?php
namespace Drupal\taxonomy\Tests; namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\views\Views; use Drupal\views\Views;
...@@ -106,7 +106,11 @@ public function testTaxonomyRss() { ...@@ -106,7 +106,11 @@ public function testTaxonomyRss() {
// Test that the feed page exists for the term. // Test that the feed page exists for the term.
$this->drupalGet("taxonomy/term/{$term1->id()}/feed"); $this->drupalGet("taxonomy/term/{$term1->id()}/feed");
$this->assertTrue(!empty($this->cssSelect('rss[version="2.0"]')), "Feed page is RSS."); $assert = $this->assertSession();
$assert->responseHeaderContains('Content-Type', 'application/rss+xml');
// Ensure the RSS version is 2.0.
$rss_array = $this->getSession()->getDriver()->find('rss');
$this->assertEquals('2.0', reset($rss_array)->getAttribute('version'));
// Check that the "Exception value" is disabled by default. // Check that the "Exception value" is disabled by default.
$this->drupalGet('taxonomy/term/all/feed'); $this->drupalGet('taxonomy/term/all/feed');
......
<?php <?php
namespace Drupal\taxonomy\Tests; namespace Drupal\Tests\taxonomy\Functional;
/** /**
* Ensure that the term indentation works properly. * Ensure that the term indentation works properly.
...@@ -33,6 +33,7 @@ protected function setUp() { ...@@ -33,6 +33,7 @@ protected function setUp() {
* Tests term indentation. * Tests term indentation.
*/ */
public function testTermIndentation() { public function testTermIndentation() {
$assert = $this->assertSession();
// Create three taxonomy terms. // Create three taxonomy terms.
$term1 = $this->createTerm($this->vocabulary); $term1 = $this->createTerm($this->vocabulary);
$term2 = $this->createTerm($this->vocabulary); $term2 = $this->createTerm($this->vocabulary);
...@@ -42,15 +43,23 @@ public function testTermIndentation() { ...@@ -42,15 +43,23 @@ public function testTermIndentation() {
$taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term'); $taxonomy_storage = $this->container->get('entity.manager')->getStorage('taxonomy_term');
// Indent the second term under the first one. // Indent the second term under the first one.
$edit = [ $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview');
$hidden_edit = [
'terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][tid]' => 2,
'terms[tid:' . $term2->id() . ':0][term][parent]' => 1, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 1,
'terms[tid:' . $term2->id() . ':0][term][depth]' => 1, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 1,
];
// Because we can't post hidden form elements, we have to change them in
// code here, and then submit.
foreach ($hidden_edit as $field => $value) {
$node = $assert->hiddenFieldExists($field);
$node->setValue($value);
}
$edit = [
'terms[tid:' . $term2->id() . ':0][weight]' => 1, 'terms[tid:' . $term2->id() . ':0][weight]' => 1,
]; ];
// Submit the edited form and check for HTML indentation element presence. // Submit the edited form and check for HTML indentation element presence.
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save')); $this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertPattern('|<div class="js-indentation indentation">&nbsp;</div>|'); $this->assertPattern('|<div class="js-indentation indentation">&nbsp;</div>|');
// Check explicitly that term 2's parent is term 1. // Check explicitly that term 2's parent is term 1.
...@@ -58,16 +67,24 @@ public function testTermIndentation() { ...@@ -58,16 +67,24 @@ public function testTermIndentation() {
$this->assertEqual(key($parents), 1, 'Term 1 is the term 2\'s parent'); $this->assertEqual(key($parents), 1, 'Term 1 is the term 2\'s parent');
// Move the second term back out to the root level. // Move the second term back out to the root level.
$edit = [ $this->drupalGet('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview');
$hidden_edit = [
'terms[tid:' . $term2->id() . ':0][term][tid]' => 2, 'terms[tid:' . $term2->id() . ':0][term][tid]' => 2,
'terms[tid:' . $term2->id() . ':0][term][parent]' => 0, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0,
'terms[tid:' . $term2->id() . ':0][term][depth]' => 0, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0,
];
// Because we can't post hidden form elements, we have to change them in
// code here, and then submit.
foreach ($hidden_edit as $field => $value) {
$node = $assert->hiddenFieldExists($field);
$node->setValue($value);
}
$edit = [
'terms[tid:' . $term2->id() . ':0][weight]' => 1, 'terms[tid:' . $term2->id() . ':0][weight]' => 1,
]; ];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->get('vid') . '/overview', $edit, t('Save'));
// All terms back at the root level, no indentation should be present. // All terms back at the root level, no indentation should be present.
$this->assertNoPattern('|<div class="js-indentation indentation">&nbsp;</div>|'); $this->assertSession()->responseNotMatches('|<div class="js-indentation indentation">&nbsp;</div>|');
// Check explicitly that term 2 has no parents. // Check explicitly that term 2 has no parents.
\Drupal::entityManager()->getStorage('taxonomy_term')->resetCache(); \Drupal::entityManager()->getStorage('taxonomy_term')->resetCache();
......
<?php <?php
namespace Drupal\taxonomy\Tests; namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay; use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
...@@ -15,6 +16,13 @@ ...@@ -15,6 +16,13 @@
*/ */
class TermAutocompleteTest extends TaxonomyTestBase { class TermAutocompleteTest extends TaxonomyTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['node'];
/** /**
* The vocabulary. * The vocabulary.
* *
...@@ -127,7 +135,25 @@ protected function setUp() { ...@@ -127,7 +135,25 @@ protected function setUp() {
// Retrieve the autocomplete url. // Retrieve the autocomplete url.
$this->drupalGet('node/add/article'); $this->drupalGet('node/add/article');
$result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]'); $result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]');
$this->autocompleteUrl = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']); $this->autocompleteUrl = $this->getAbsoluteUrl($result[0]->getAttribute('data-autocomplete-path'));
}
/**
* Helper function for JSON formatted requests.
*
* @param string|\Drupal\Core\Url $path
* Drupal path or URL to load into Mink controlled browser.
* @param array $options
* (optional) Options to be forwarded to the url generator.
* @param string[] $headers
* (optional) An array containing additional HTTP request headers.
*
* @return string[]
* Array representing decoded JSON response.
*/
protected function drupalGetJson($path, array $options = [], array $headers = []) {
$options = array_merge_recursive(['query' => ['_format' => 'json']], $options);
return Json::decode($this->drupalGet($path, $options, $headers));
} }
/** /**
...@@ -137,21 +163,21 @@ protected function setUp() { ...@@ -137,21 +163,21 @@ protected function setUp() {
*/ */
public function testAutocompleteCountResults() { public function testAutocompleteCountResults() {
// Test that no matching term found. // Test that no matching term found.
$data = $this->drupalGetJSON( $data = $this->drupalGetJson(
$this->autocompleteUrl, $this->autocompleteUrl,
['query' => ['q' => 'zzz']] ['query' => ['q' => 'zzz']]
); );
$this->assertTrue(empty($data), 'Autocomplete returned no results'); $this->assertTrue(empty($data), 'Autocomplete returned no results');
// Test that only one matching term found, when only one matches. // Test that only one matching term found, when only one matches.
$data = $this->drupalGetJSON( $data = $this->drupalGetJson(
$this->autocompleteUrl, $this->autocompleteUrl,
['query' => ['q' => 'aaa 10']] ['query' => ['q' => 'aaa 10']]
); );
$this->assertEqual(1, count($data), 'Autocomplete returned 1 result'); $this->assertEqual(1, count($data), 'Autocomplete returned 1 result');
// Test the correct number of matches when multiple are partial matches. // Test the correct number of matches when multiple are partial matches.
$data = $this->drupalGetJSON( $data = $this->drupalGetJson(
$this->autocompleteUrl, $this->autocompleteUrl,
['query' => ['q' => 'aaa 1']] ['query' => ['q' => 'aaa 1']]
); );
...@@ -159,7 +185,7 @@ public function testAutocompleteCountResults() { ...@@ -159,7 +185,7 @@ public function testAutocompleteCountResults() {
// Tests that only 10 results are returned, even if there are more than 10 // Tests that only 10 results are returned, even if there are more than 10
// matches. // matches.
$data = $this->drupalGetJSON( $data = $this->drupalGetJson(
$this->autocompleteUrl, $this->autocompleteUrl,
['query' => ['q' => 'aaa']] ['query' => ['q' => 'aaa']]
); );
...@@ -192,7 +218,7 @@ public function testAutocompleteOrderedResults() { ...@@ -192,7 +218,7 @@ public function testAutocompleteOrderedResults() {
]; ];
} }
$data = $this->drupalGetJSON( $data = $this->drupalGetJson(
$this->autocompleteUrl, $this->autocompleteUrl,
['query' => ['q' => 'bbb']] ['query' => ['q' => 'bbb']]
); );
......
<?php <?php
namespace Drupal\taxonomy\Tests; namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Component\Utility\Tags; use Drupal\Component\Utility\Tags;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
...@@ -389,6 +389,7 @@ public function testTermInterface() { ...@@ -389,6 +389,7 @@ public function testTermInterface() {
* Save, edit and delete a term using the user interface. * Save, edit and delete a term using the user interface.
*/ */
public function testTermReorder() { public function testTermReorder() {
$assert = $this->assertSession();
$this->createTerm($this->vocabulary); $this->createTerm($this->vocabulary);
$this->createTerm($this->vocabulary); $this->createTerm($this->vocabulary);
$this->createTerm($this->vocabulary); $this->createTerm($this->vocabulary);
...@@ -406,21 +407,30 @@ public function testTermReorder() { ...@@ -406,21 +407,30 @@ public function testTermReorder() {
// "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2, // "tid:1:0[depth]", and "tid:1:0[weight]". Change the order to term2,
// term3, term1 by setting weight property, make term3 a child of term2 by // term3, term1 by setting weight property, make term3 a child of term2 by
// setting the parent and depth properties, and update all hidden fields. // setting the parent and depth properties, and update all hidden fields.
$edit = [ $hidden_edit = [
'terms[tid:' . $term2->id() . ':0][term][tid]' => $term2->id(), 'terms[tid:' . $term2->id() . ':0][term][tid]' => $term2->id(),
'terms[tid:' . $term2->id() . ':0][term][parent]' => 0, 'terms[tid:' . $term2->id() . ':0][term][parent]' => 0,
'terms[tid:' . $term2->id() . ':0][term][depth]' => 0, 'terms[tid:' . $term2->id() . ':0][term][depth]' => 0,
'terms[tid:' . $term2->id() . ':0][weight]' => 0,
'terms[tid:' . $term3->id() . ':0][term][tid]' => $term3->id(), 'terms[tid:' . $term3->id() . ':0][term][tid]' => $term3->id(),
'terms[tid:' . $term3->id() . ':0][term][parent]' => $term2->id(), 'terms[tid:' . $term3->id() . ':0][term][parent]' => $term2->id(),
'terms[tid:' . $term3->id() . ':0][term][depth]' => 1, 'terms[tid:' . $term3->id() . ':0][term][depth]' => 1,
'terms[tid:' . $term3->id() . ':0][weight]' => 1,
'terms[tid:' . $term1->id() . ':0][term][tid]' => $term1->id(), 'terms[tid:' . $term1->id() . ':0][term][tid]' => $term1->id(),
'terms[tid:' . $term1->id() . ':0][term][parent]' => 0, 'terms[tid:' . $term1->id() . ':0][term][parent]' => 0,
'terms[tid:' . $term1->id() . ':0][term][depth]' => 0, 'terms[tid:' . $term1->id() . ':0][term][depth]' => 0,
];
// Because we can't post hidden form elements, we have to change them in
// code here, and then submit.
foreach ($hidden_edit as $field => $value) {
$node = $assert->hiddenFieldExists($field);
$node->setValue($value);
}
// Edit non-hidden elements within drupalPostForm().
$edit = [
'terms[tid:' . $term2->id() . ':0][weight]' => 0,
'terms[tid:' . $term3->id() . ':0][weight]' => 1,
'terms[tid:' . $term1->id() . ':0][weight]' => 2, 'terms[tid:' . $term1->id() . ':0][weight]' => 2,
]; ];
$this->drupalPostForm(NULL, $edit, t('Save')); $this->drupalPostForm(NULL, $edit, 'Save');
$taxonomy_storage->resetCache(); $taxonomy_storage->resetCache();
$terms = $taxonomy_storage->loadTree($this->vocabulary->id()); $terms = $taxonomy_storage->loadTree($this->vocabulary->id());
...@@ -570,7 +580,7 @@ public function testTermBreadcrumbs() { ...@@ -570,7 +580,7 @@ public function testTermBreadcrumbs() {
]; ];
// Create the term. // Create the term.
$this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, t('Save')); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $this->vocabulary->id() . '/add', $edit, 'Save');
$terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']); $terms = taxonomy_term_load_multiple_by_name($edit['name[0][value]']);
$term = reset($terms); $term = reset($terms);
...@@ -578,19 +588,19 @@ public function testTermBreadcrumbs() { ...@@ -578,19 +588,19 @@ public function testTermBreadcrumbs() {
// Check the breadcrumb on the term edit page. // Check the breadcrumb on the term edit page.
$this->drupalGet('taxonomy/term/' . $term->id() . '/edit'); $this->drupalGet('taxonomy/term/' . $term->id() . '/edit');
$breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a'); $breadcrumbs = $this->getSession()->getPage()->findAll('css', 'nav.breadcrumb ol li a');
$this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.'); $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
$this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home'); $this->assertIdentical($breadcrumbs[0]->getText(), 'Home', 'First breadcrumb text is Home');
$this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term edit page.'); $this->assertIdentical($breadcrumbs[1]->getText(), $term->label(), 'Second breadcrumb text is term name on term edit page.');
$this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.'); $this->assertEscaped($breadcrumbs[1]->getText(), 'breadcrumbs displayed and escaped.');
// Check the breadcrumb on the term delete page. // Check the breadcrumb on the term delete page.
$this->drupalGet('taxonomy/term/' . $term->id() . '/delete'); $this->drupalGet('taxonomy/term/' . $term->id() . '/delete');
$breadcrumbs = $this->cssSelect('nav.breadcrumb ol li a'); $breadcrumbs = $this->getSession()->getPage()->findAll('css', 'nav.breadcrumb ol li a');
$this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.'); $this->assertIdentical(count($breadcrumbs), 2, 'The breadcrumbs are present on the page.');
$this->assertIdentical((string) $breadcrumbs[0], 'Home', 'First breadcrumb text is Home'); $this->assertIdentical($breadcrumbs[0]->getText(), 'Home', 'First breadcrumb text is Home');
$this->assertIdentical((string) $breadcrumbs[1], $term->label(), 'Second breadcrumb text is term name on term delete page.'); $this->assertIdentical($breadcrumbs[1]->getText(), $term->label(), 'Second breadcrumb text is term name on term delete page.');
$this->assertEscaped((string) $breadcrumbs[1], 'breadcrumbs displayed and escaped.'); $this->assertEscaped($breadcrumbs[1]->getText(), 'breadcrumbs displayed and escaped.');
} }
} }
<?php <?php
namespace Drupal\taxonomy\Tests; namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\system\Tests\Menu\AssertBreadcrumbTrait; use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
/** /**
* Tests for proper breadcrumb translation. * Tests for proper breadcrumb translation.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment