View source
<?php
namespace Drupal\Tests\search\Functional;
use Drupal\Tests\BrowserTestBase;
class SearchExactTest extends BrowserTestBase {
protected static $modules = [
'node',
'search',
];
protected $defaultTheme = 'stark';
public function testExactQuery() {
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Basic page',
]);
$user = $this
->drupalCreateUser([
'create page content',
'search content',
]);
$this
->drupalLogin($user);
$settings = [
'type' => 'page',
'title' => 'Simple Node',
];
for ($i = 0; $i <= 17; $i++) {
$settings['body'] = [
[
'value' => 'love pizza',
],
];
$this
->drupalCreateNode($settings);
}
for ($i = 0; $i <= 17; $i++) {
$settings['body'] = [
[
'value' => 'love cheesy pizza',
],
];
$this
->drupalCreateNode($settings);
}
$settings['body'] = [
[
'value' => 'Druplicon',
],
];
$node = $this
->drupalCreateNode($settings);
$this->container
->get('plugin.manager.search')
->createInstance('node_search')
->updateIndex();
$this
->refreshVariables();
$edit = [
'keys' => 'love pizza',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertLinkByHref('page=1', 0, '2nd page link is found for keyword search.');
$this
->assertLinkByHref('page=2', 0, '3rd page link is found for keyword search.');
$this
->assertLinkByHref('page=3', 0, '4th page link is found for keyword search.');
$this
->assertNoLinkByHref('page=4', '5th page link is not found for keyword search.');
$edit = [
'keys' => '"love pizza"',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertLinkByHref('page=1', 0, '2nd page link is found for exact phrase search.');
$this
->assertNoLinkByHref('page=2', '3rd page link is not found for exact phrase search.');
$node_type_config = \Drupal::configFactory()
->getEditable('node.type.page');
$node_type_config
->set('display_submitted', TRUE);
$node_type_config
->save();
$edit = [
'keys' => 'Druplicon',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertText($user
->getAccountName(), 'Basic page node displays author name when post settings are on.');
$this
->assertText($this->container
->get('date.formatter')
->format($node
->getChangedTime(), 'short'), 'Basic page node displays post date when post settings are on.');
$node_type_config
->set('display_submitted', FALSE);
$node_type_config
->save();
$edit = [
'keys' => 'Druplicon',
];
$this
->drupalPostForm('search/node', $edit, t('Search'));
$this
->assertNoText($user
->getAccountName(), 'Basic page node does not display author name when post settings are off.');
$this
->assertNoText($this->container
->get('date.formatter')
->format($node
->getChangedTime(), 'short'), 'Basic page node does not display post date when post settings are off.');
}
}