Unverified Commit ad901646 authored by alexpott's avatar alexpott

Issue #2863842 by GoZ, Lendude, vaplas, dawehner, alexpott, borisson_,...

Issue #2863842 by GoZ, Lendude, vaplas, dawehner, alexpott, borisson_, larowlan: Convert web tests to browser tests for search module
parent 845da749
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Indexes content and tests the advanced search form.
......@@ -53,14 +53,14 @@ public function testNodeType() {
// Search for the title of the node with a POST query.
$edit = ['or' => $this->node->label()];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
$this->assertText($this->node->label(), 'Basic page node is found with POST query.');
// Search by node type.
$this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), t('Advanced search'));
$this->drupalPostForm('search/node', array_merge($edit, ['type[page]' => 'page']), 'edit-submit--2');
$this->assertText($this->node->label(), 'Basic page node is found with POST query and type:page.');
$this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), t('Advanced search'));
$this->drupalPostForm('search/node', array_merge($edit, ['type[article]' => 'article']), 'edit-submit--2');
$this->assertText('search yielded no results', 'Article node is not found with POST query and type:article.');
}
......@@ -75,7 +75,7 @@ public function testFormRefill() {
'negative' => 'fish snake',
'type[page]' => 'page',
];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Test that the encoded query appears in the page title. Only test the
// part not including the quote, because assertText() cannot seem to find
......@@ -86,11 +86,11 @@ public function testFormRefill() {
foreach ($edit as $key => $value) {
if ($key != 'type[page]') {
$elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
$this->assertTrue(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is set to $value");
$this->assertTrue(isset($elements[0]) && $elements[0]->getValue() == $value, "Field $key is set to $value");
}
else {
$elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
$this->assertTrue(isset($elements[0]) && !empty($elements[0]['checked']), "Field $key is checked");
$this->assertTrue(isset($elements[0]) && !empty($elements[0]->getAttribute('checked')), "Field $key is checked");
}
}
......@@ -98,12 +98,12 @@ public function testFormRefill() {
// search box, and verify that the advanced form is not filled out.
// (It shouldn't be filled out unless you submit values in those fields.)
$edit2 = ['keys' => 'cat dog OR gerbil -fish -snake'];
$this->drupalPostForm('search/node', $edit2, t('Advanced search'));
$this->drupalPostForm('search/node', $edit2, 'edit-submit--2');
$this->assertText('Search for cat dog OR gerbil -fish -snake');
foreach ($edit as $key => $value) {
if ($key != 'type[page]') {
$elements = $this->xpath('//input[@name=:name]', [':name' => $key]);
$this->assertFalse(isset($elements[0]) && $elements[0]['value'] == $value, "Field $key is not set to $value");
$this->assertFalse(isset($elements[0]) && $elements[0]->getValue() == $value, "Field $key is not set to $value");
}
}
}
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Tests if the search form block is available.
......@@ -31,7 +31,7 @@ public function testSearchFormBlock() {
// Test availability of the search block in the admin "Place blocks" list.
$this->drupalGet('admin/structure/block');
$this->clickLinkPartialName('Place block');
$this->getSession()->getPage()->findLink('Place block')->click();
$this->assertLinkByHref('/admin/structure/block/add/search_form_block/classy', 0,
'Did not find the search block in block candidate list.');
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Behat\Mink\Exception\ResponseTextException;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\Traits\Core\CronRunTrait;
use Drupal\user\RoleInterface;
use Drupal\filter\Entity\FilterFormat;
......@@ -16,6 +18,7 @@
class SearchCommentTest extends SearchTestBase {
use CommentTestTrait;
use CronRunTrait;
/**
* Modules to enable.
......@@ -298,15 +301,18 @@ public function assertCommentAccess($assume_access, $message) {
];
$this->drupalPostForm('search/node', $edit, t('Search'));
if ($assume_access) {
$expected_node_result = $this->assertText($this->node->label());
$expected_comment_result = $this->assertText($this->commentSubject);
try {
if ($assume_access) {
$this->assertSession()->pageTextContains($this->node->label());
$this->assertSession()->pageTextContains($this->commentSubject);
}
else {
$this->assertSession()->pageTextContains(t('Your search yielded no results.'));
}
}
else {
$expected_node_result = $this->assertText(t('Your search yielded no results.'));
$expected_comment_result = $this->assertText(t('Your search yielded no results.'));
catch (ResponseTextException $exception) {
$this->fail($message);
}
$this->assertTrue($expected_node_result && $expected_comment_result, $message);
}
/**
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Url;
use Drupal\search\Entity\SearchPage;
......@@ -217,9 +217,9 @@ public function testSearchModuleDisabling() {
public function testDefaultSearchPageOrdering() {
$this->drupalGet('search');
$elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
$this->assertIdentical((string) $elements[0]['href'], \Drupal::url('search.view_node_search'));
$this->assertIdentical((string) $elements[1]['href'], \Drupal::url('search.view_dummy_search_type'));
$this->assertIdentical((string) $elements[2]['href'], \Drupal::url('search.view_user_search'));
$this->assertIdentical($elements[0]->getAttribute('href'), \Drupal::url('search.view_node_search'));
$this->assertIdentical($elements[1]->getAttribute('href'), \Drupal::url('search.view_dummy_search_type'));
$this->assertIdentical($elements[2]->getAttribute('href'), \Drupal::url('search.view_user_search'));
}
/**
......@@ -272,8 +272,8 @@ public function testMultipleSearchPages() {
// Ensure both search pages have their tabs displayed.
$this->drupalGet('search');
$elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
$this->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $first_id)->toString());
$this->assertIdentical((string) $elements[1]['href'], Url::fromRoute('search.view_' . $second_id)->toString());
$this->assertIdentical($elements[0]->getAttribute('href'), Url::fromRoute('search.view_' . $first_id)->toString());
$this->assertIdentical($elements[1]->getAttribute('href'), Url::fromRoute('search.view_' . $second_id)->toString());
// Switch the weight of the search pages and check the order of the tabs.
$edit = [
......@@ -283,8 +283,8 @@ public function testMultipleSearchPages() {
$this->drupalPostForm('admin/config/search/pages', $edit, t('Save configuration'));
$this->drupalGet('search');
$elements = $this->xpath('//*[contains(@class, :class)]//a', [':class' => 'tabs primary']);
$this->assertIdentical((string) $elements[0]['href'], Url::fromRoute('search.view_' . $second_id)->toString());
$this->assertIdentical((string) $elements[1]['href'], Url::fromRoute('search.view_' . $first_id)->toString());
$this->assertIdentical($elements[0]->getAttribute('href'), Url::fromRoute('search.view_' . $second_id)->toString());
$this->assertIdentical($elements[1]->getAttribute('href'), Url::fromRoute('search.view_' . $first_id)->toString());
// Check the initial state of the search pages.
$this->drupalGet('admin/config/search/pages');
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Verifies that a form embedded in search results works.
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\language\Entity\ConfigurableLanguage;
......@@ -99,12 +99,12 @@ public function testLanguages() {
$this->assertText(t('French'), 'French is a possible choice.');
// Ensure selecting no language does not make the query different.
$this->drupalPostForm('search/node', [], t('Advanced search'));
$this->drupalPostForm('search/node', [], 'edit-submit--2');
$this->assertUrl(\Drupal::url('search.view_node_search', [], ['query' => ['keys' => ''], 'absolute' => TRUE]), [], 'Correct page redirection, no language filtering.');
// Pick French and ensure it is selected.
$edit = ['language[fr]' => TRUE];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Get the redirected URL.
$url = $this->getUrl();
$parts = parse_url($url);
......@@ -113,7 +113,7 @@ public function testLanguages() {
// Search for keyword node and language filter as Spanish.
$edit = ['keys' => 'node', 'language[es]' => TRUE];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Check for Spanish results.
$this->assertLink('Second node this is the Spanish title', 0, 'Second node Spanish title found in search results');
$this->assertLink('Third node es', 0, 'Third node Spanish found in search results');
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Tests search index is updated properly when nodes are removed or updated.
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
* Tests that numbers can be searched with more complex matching.
......@@ -10,6 +11,9 @@
* @group search
*/
class SearchNumberMatchingTest extends SearchTestBase {
use CronRunTrait;
/**
* A user with permission to administer nodes.
*
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
* Tests that numbers can be searched.
......@@ -10,6 +11,9 @@
* @group search
*/
class SearchNumbersTest extends SearchTestBase {
use CronRunTrait;
/**
* A user with permission to administer nodes.
*
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\Tests\system\Functional\Cache\AssertPageCacheContextsAndTagsTrait;
/**
* Tests the search_page entity cache tags on the search results pages.
......@@ -11,6 +12,8 @@
*/
class SearchPageCacheTagsTest extends SearchTestBase {
use AssertPageCacheContextsAndTagsTrait;
/**
* {@inheritdoc}
*/
......@@ -100,8 +103,8 @@ public function testSearchText() {
$this->drupalGet('search/user');
$this->assertCacheTag('config:search.page.user_search');
$this->assertCacheTag('user_list');
$this->assertNoCacheTag('search_index');
$this->assertNoCacheTag('search_index:user_search');
$this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
$this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
// User search results.
$edit['keys'] = $this->searchingUser->getUsername();
......@@ -109,8 +112,8 @@ public function testSearchText() {
$this->assertCacheTag('config:search.page.user_search');
$this->assertCacheTag('user_list');
$this->assertCacheTag('user:2');
$this->assertNoCacheTag('search_index');
$this->assertNoCacheTag('search_index:user_search');
$this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
$this->assertSession()->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
}
/**
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Unicode;
......@@ -90,7 +90,7 @@ public function testSearchText() {
$search_terms = 'Hear nothing > "see nothing" `feel' . " '1982.";
$edit['keys'] = $search_terms;
$this->drupalPostForm('search/node', $edit, t('Search'));
$actual_title = (string) current($this->xpath('//title'));
$actual_title = $this->xpath('//title')[0]->getText();
$this->assertEqual($actual_title, Html::decodeEntities(t($title_source, ['@keywords' => Unicode::truncate($search_terms, 60, TRUE, TRUE)])), 'Search page title is correct');
$edit['keys'] = $this->searchingUser->getUsername();
......@@ -133,9 +133,9 @@ public function testSearchText() {
// Make sure the "Please enter some keywords" message is NOT displayed if
// you use "or" words or phrases in Advanced Search.
$this->drupalPostForm('search/node', ['or' => $this->randomMachineName() . ' ' . $this->randomMachineName()], t('Advanced search'));
$this->drupalPostForm('search/node', ['or' => $this->randomMachineName() . ' ' . $this->randomMachineName()], 'edit-submit--2');
$this->assertNoText(t('Please enter some keywords'), 'With advanced OR keywords entered, no keywords message is not displayed on node page');
$this->drupalPostForm('search/node', ['phrase' => '"' . $this->randomMachineName() . '" "' . $this->randomMachineName() . '"'], t('Advanced search'));
$this->drupalPostForm('search/node', ['phrase' => '"' . $this->randomMachineName() . '" "' . $this->randomMachineName() . '"'], 'edit-submit--2');
$this->assertNoText(t('Please enter some keywords'), 'With advanced phrase entered, no keywords message is not displayed on node page');
// Verify that if you search for a too-short keyword, you get the right
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Tests that the search preprocessing uses the correct language code.
......@@ -54,7 +54,7 @@ public function testPreprocessLangcode() {
// function. If you search for text that is in the node, preprocess is
// not invoked on the node during the search excerpt generation.
$edit = ['or' => 'Additional text'];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Checks if the langcode message has been set by hook_search_preprocess().
$this->assertText('Langcode Preprocess Test: en');
......@@ -81,7 +81,7 @@ public function testPreprocessStemming() {
// Search for the title of the node with a POST query.
$edit = ['or' => 'testing'];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Check if the node has been found.
$this->assertText('Search results');
......@@ -89,7 +89,7 @@ public function testPreprocessStemming() {
// Search for the same node using a different query.
$edit = ['or' => 'test'];
$this->drupalPostForm('search/node', $edit, t('Advanced search'));
$this->drupalPostForm('search/node', $edit, 'edit-submit--2');
// Check if the node has been found.
$this->assertText('Search results');
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
/**
* Tests that the node search query can be altered via the query alter hook.
......
<?php
namespace Drupal\search\Tests;
namespace Drupal\Tests\search\Functional;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Url;
use Drupal\filter\Entity\FilterFormat;
use Drupal\search\Entity\SearchPage;
use Drupal\Tests\Traits\Core\CronRunTrait;
/**
* Indexes content and tests ranking factors.
......@@ -16,6 +17,7 @@
class SearchRankingTest extends SearchTestBase {
use CommentTestTrait;
use CronRunTrait;
/**
* The node search page.
......
......@@ -3,7 +3,6 @@
namespace Drupal\Tests\search\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Component\Render\FormattableMarkup;
/**
* Defines the common search test code.
......@@ -28,17 +27,7 @@ protected function setUp() {
}
/**
* Simulates submission of a form using GET instead of POST.
*
* Forms that use the GET method cannot be submitted with
* WebTestBase::drupalPostForm(), which explicitly uses POST to submit the
* form. So this method finds the form, verifies that it has input fields and
* a submit button matching the inputs to this method, and then calls
* WebTestBase::drupalGet() to simulate the form submission to the 'action'
* URL of the form (if set, or the current URL if not).
*
* See WebTestBase::drupalPostForm() for more detailed documentation of the
* function parameters.
* Submission of a form via press submit button.
*
* @param string $path
* Location of the form to be submitted: either a Drupal path, absolute
......@@ -51,42 +40,27 @@ protected function setUp() {
* this does not support AJAX.
* @param string $form_html_id
* (optional) HTML ID of the form, to disambiguate.
*
* @throws \Behat\Mink\Exception\ElementNotFoundException
*
* @todo: Replace after https://www.drupal.org/project/drupal/issues/2887411
*/
protected function submitGetForm($path, $edit, $submit, $form_html_id = NULL) {
if (isset($path)) {
$this->drupalGet($path);
}
if ($this->parse()) {
// Iterate over forms to find one that matches $edit and $submit.
$edit_save = $edit;
$xpath = '//form';
if (!empty($form_html_id)) {
$xpath .= "[@id='" . $form_html_id . "']";
}
$forms = $this->xpath($xpath);
foreach ($forms as $form) {
// Try to set the fields of this form as specified in $edit.
$edit = $edit_save;
$post = [];
$upload = [];
$submit_matches = $this->handleForm($post, $edit, $upload, $submit, $form);
if (!$edit && $submit_matches) {
// Everything matched, so "submit" the form.
$action = isset($form['action']) ? $this->getAbsoluteUrl((string) $form['action']) : NULL;
$this->drupalGet($action, ['query' => $post]);
return;
}
}
// We have not found a form which contained all fields of $edit and
// the submit button.
foreach ($edit as $name => $value) {
$this->fail(new FormattableMarkup('Failed to set field @name to @value', ['@name' => $name, '@value' => $value]));
}
$this->assertTrue($submit_matches, format_string('Found the @submit button', ['@submit' => $submit]));
$this->fail(format_string('Found the requested form fields at @path', ['@path' => $path]));
$page = $this->getSession()->getPage();
$wrapper = $page;
if ($form_html_id) {
$wrapper = $page->find('css', '#' . $form_html_id);
}
$button = $wrapper->findButton($submit);
$form = $this->assertSession()->elementExists('xpath', './ancestor::form', $button);
foreach ($edit as $selector => $value) {
$form->fillField($selector, $value);
}
$button->press();
}
}
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