View source
<?php
namespace Drupal\Tests\facets\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Url;
trait TestHelperTrait {
protected function assertFacetLabel($label, $index = 0, $message = '', $group = 'Other') {
$links = $this
->findFacetLink($label);
$message = $message ? $message : strtr('Link with label %label found.', [
'%label' => $label,
]);
return $this
->assertArrayHasKey($index, $links, $message, $group);
}
protected function checkFacetIsActive($label) {
$links = $this
->findFacetLink($label);
return $this
->assertArrayHasKey(0, $links);
}
protected function checkFacetIsNotActive($label) {
$label = (string) $label;
$label = strip_tags($label);
$links = $this
->xpath('//a/span[1][normalize-space(text())=:label]', [
':label' => $label,
]);
return $this
->assertArrayHasKey(0, $links);
}
protected function assertNoFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$xpath = $this
->xpath('//div[@id = :id]/div[@class="facet-empty"]', [
':id' => 'block-' . $block
->id(),
]);
if (!$xpath) {
$this
->assertEmpty($xpath);
}
else {
$this
->assertTrue($this
->xpath('//div[@id = :id]/div[@class="facet-empty"]', [
':id' => 'block-' . $block
->id(),
]));
}
}
}
protected function assertFacetBlocksAppear() {
foreach ($this->blocks as $block) {
$this
->xpath('//div[@id = :id]', [
':id' => 'block-' . $block
->id(),
]);
$this
->assertSession()
->pageTextContains($block
->label());
}
}
protected function assertStringPosition($x, $y) {
$this
->assertSession()
->pageTextContains($x);
$this
->assertSession()
->pageTextContains($y);
$x_position = strpos($this
->getTextContent(), $x);
$y_position = strpos($this
->getTextContent(), $y);
$message = new FormattableMarkup('Assert that %x is before %y in the source', [
'%x' => $x,
'%y' => $y,
]);
$this
->assertTrue($x_position < $y_position, $message);
}
protected function checkClickedFacetUrl(Url $url) {
$this
->drupalGet('search-api-test-fulltext');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertFacetLabel('item');
$this
->assertFacetLabel('article');
$this
->clickLink('item');
$this
->assertSession()
->statusCodeEquals(200);
$this
->checkFacetIsActive('item');
$this
->assertFacetLabel('article');
$this
->assertSession()
->addressEquals($url);
}
protected function clickPartialLink($label) {
$label = (string) $label;
$xpath = $this
->assertSession()
->buildXPathQuery('//a[starts-with(normalize-space(), :label)]', [
':label' => $label,
]);
$links = $this
->getSession()
->getPage()
->findAll('xpath', $xpath);
$links[0]
->click();
}
protected function findFacetLink($label) {
$label = (string) $label;
$label = strip_tags($label);
$matches = [];
if (preg_match('/(.*)\\s\\((\\d+)\\)/', $label, $matches)) {
$links = $this
->xpath('//a//span[normalize-space(text())=:label]/following-sibling::span[normalize-space(text())=:count]', [
':label' => $matches[1],
':count' => '(' . $matches[2] . ')',
]);
}
else {
$links = $this
->xpath('//a//span[normalize-space(text())=:label]', [
':label' => $label,
]);
}
return $links;
}
protected function convertNameToMachineName($facet_name) {
return preg_replace('@[^a-zA-Z0-9_]+@', '_', strtolower($facet_name));
}
}