SearchIntegrationTest.php in Drupal 8
File
core/modules/views/tests/src/Functional/SearchIntegrationTest.php
View source
<?php
namespace Drupal\Tests\views\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Tests\Traits\Core\CronRunTrait;
class SearchIntegrationTest extends ViewTestBase {
use CronRunTrait;
public static $modules = [
'node',
'search',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_search',
];
public function testSearchIntegration() {
$type = $this
->drupalCreateContentType();
$node['title'] = 'pizza';
$node['body'] = [
[
'value' => 'pizza',
],
];
$node['type'] = $type
->id();
$this
->drupalCreateNode($node);
$this
->drupalGet('node/1');
$node_url = $this
->getUrl();
$node['title'] = 'sandwich';
$node['body'] = [
[
'value' => 'sandwich with a <a href="' . $node_url . '">link to first node</a>',
],
];
$this
->drupalCreateNode($node);
$node['title'] = 'cola';
$node['body'] = [
[
'value' => 'cola is good with pizza',
],
];
$node['type'] = $type
->id();
$this
->drupalCreateNode($node);
$this
->cronRun();
$this
->drupalGet('test-filter');
$this
->assertSession()
->linkExists('pizza');
$this
->assertSession()
->linkNotExists('sandwich');
$this
->assertSession()
->linkExists('cola');
$this
->drupalGet('test-arg/pizza');
$this
->assertOneLink('pizza');
$this
->assertSession()
->linkNotExists('sandwich');
$this
->assertOneLink('cola');
$this
->drupalGet('test-arg/sandwich');
$this
->assertSession()
->linkNotExists('pizza');
$this
->assertOneLink('sandwich');
$this
->assertSession()
->linkNotExists('cola');
$this
->drupalGet('test-arg/pizza OR sandwich');
$this
->assertOneLink('pizza');
$this
->assertOneLink('sandwich');
$this
->assertOneLink('cola');
$this
->drupalGet('test-arg/pizza sandwich OR cola');
$this
->assertSession()
->linkNotExists('pizza');
$this
->assertSession()
->linkNotExists('sandwich');
$this
->assertOneLink('cola');
$this
->drupalGet('test-arg/cola pizza');
$this
->assertSession()
->linkNotExists('pizza');
$this
->assertSession()
->linkNotExists('sandwich');
$this
->assertOneLink('cola');
$this
->drupalGet('test-arg/"cola is good"');
$this
->assertSession()
->linkNotExists('pizza');
$this
->assertSession()
->linkNotExists('sandwich');
$this
->assertOneLink('cola');
$node = [
'title' => "Drupal's search rocks.",
'type' => $type
->id(),
];
$this
->drupalCreateNode($node);
$node['title'] = "Drupal's search rocks <em>really</em> rocks!";
$this
->drupalCreateNode($node);
$this
->cronRun();
$this
->drupalGet('test-arg/rocks');
$xpath = '//div[@class="views-row"]//a';
$results = $this
->xpath($xpath);
$this
->assertEqual($results[0]
->getText(), "Drupal's search rocks <em>really</em> rocks!");
$this
->assertEqual($results[1]
->getText(), "Drupal's search rocks.");
$this
->assertEscaped("Drupal's search rocks <em>really</em> rocks!");
$node = [
'title' => "Testing one two two two",
'type' => $type
->id(),
];
$this
->drupalCreateNode($node);
$node['title'] = "Testing one one one";
$this
->drupalCreateNode($node);
$this
->cronRun();
$this
->drupalGet('test-arg/one');
$xpath = '//div[@class="views-row"]//a';
$results = $this
->xpath($xpath);
$this
->assertEqual($results[0]
->getText(), "Testing one one one");
$this
->assertEqual($results[1]
->getText(), "Testing one two two two");
}
protected function assertOneLink($label) {
$links = $this
->xpath('//a[normalize-space(text())=:label]', [
':label' => $label,
]);
$message = new FormattableMarkup('Link with label %label found once.', [
'%label' => $label,
]);
return $this
->assert(isset($links[0]) && !isset($links[1]), $message);
}
}