Same filename and directory in other branches
- 8.9.x core/modules/book/tests/src/Functional/BookTestTrait.php
- 9 core/modules/book/tests/src/Functional/BookTestTrait.php
Namespace
Drupal\Tests\book\Functional
File
core/modules/book/tests/src/Functional/BookTestTrait.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\book\Functional;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
trait BookTestTrait {
protected $book;
protected $bookAuthor;
public function createBook($edit = []) {
$this
->drupalLogin($this->bookAuthor);
$this->book = $this
->createBookNode('new', NULL, $edit);
$book = $this->book;
$nodes = [];
$nodes[] = $this
->createBookNode($book
->id(), NULL, $edit);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[0]->book['nid'], $edit);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[0]->book['nid'], $edit);
$nodes[] = $this
->createBookNode($book
->id(), NULL, $edit);
$nodes[] = $this
->createBookNode($book
->id(), NULL, $edit);
$this
->drupalLogout();
return $nodes;
}
public function checkBookNode(EntityInterface $node, $nodes, $previous, $up, $next, array $breadcrumb) {
$this
->drupalGet('node/' . $node
->id());
if ($nodes !== NULL) {
$book_navigation = $this
->getSession()
->getPage()
->find('css', sprintf('nav[aria-labelledby="book-label-%s"] ul', $this->book
->id()));
$this
->assertNotNull($book_navigation);
$links = $book_navigation
->findAll('css', 'a');
$this
->assertCount(count($nodes), $links);
foreach ($nodes as $delta => $node) {
$link = $links[$delta];
$this
->assertEquals($node
->label(), $link
->getText());
$this
->assertEquals($node
->toUrl()
->toString(), $link
->getAttribute('href'));
}
}
if ($previous) {
$previous_element = $this
->assertSession()
->elementExists('named_exact', [
'link',
'Go to previous page',
]);
$this
->assertEquals($previous
->toUrl()
->toString(), $previous_element
->getAttribute('href'));
}
if ($up) {
$parent_element = $this
->assertSession()
->elementExists('named_exact', [
'link',
'Go to parent page',
]);
$this
->assertEquals($up
->toUrl()
->toString(), $parent_element
->getAttribute('href'));
}
if ($next) {
$next_element = $this
->assertSession()
->elementExists('named_exact', [
'link',
'Go to next page',
]);
$this
->assertEquals($next
->toUrl()
->toString(), $next_element
->getAttribute('href'));
}
$expected_breadcrumb = [];
$expected_breadcrumb[] = Url::fromRoute('<front>')
->toString();
foreach ($breadcrumb as $a_node) {
$expected_breadcrumb[] = $a_node
->toUrl()
->toString();
}
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link
->getAttribute('href');
}
$this
->assertSame($expected_breadcrumb, $got_breadcrumb, 'The breadcrumb is correctly displayed on the page.');
$this
->drupalGet('book/export/html/' . $node
->id());
$this
->assertSession()
->pageTextContains($node
->label());
$this
->assertSession()
->responseContains($node->body->processed);
}
public function generateOutlinePattern($nodes) {
@trigger_error(__METHOD__ . ' is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use methods from \\Drupal\\Tests\\WebAssert instead. See https://www.drupal.org/node/3325904', E_USER_DEPRECATED);
$outline = '';
foreach ($nodes as $node) {
$outline .= '(node\\/' . $node
->id() . ')(.*?)(' . $node
->label() . ')(.*?)';
}
return '/<nav role="navigation" aria-labelledby="book-label-' . $this->book
->id() . '"(.*?)<ul(.*?)' . $outline . '<\\/ul>/s';
}
public function createBookNode($book_nid, $parent = NULL, $edit = []) {
static $number = 0;
$edit['title[0][value]'] = str_pad((string) $number, 2, '0', STR_PAD_LEFT) . ' - test node ' . $this
->randomMachineName(10);
$edit['body[0][value]'] = 'test body ' . $this
->randomMachineName(32) . ' ' . $this
->randomMachineName(32);
$edit['book[bid]'] = $book_nid;
if ($parent !== NULL) {
$this
->drupalGet('node/add/book');
$this
->submitForm($edit, 'Change book (update list of parents)');
$edit['book[pid]'] = $parent;
$this
->submitForm($edit, 'Save');
$parent_node = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($parent);
$this
->assertNotEmpty($parent_node->book['has_children'], 'Parent node is marked as having children');
}
else {
$this
->drupalGet('node/add/book');
$this
->submitForm($edit, 'Save');
}
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node === FALSE ? NULL : $node, 'Book node found in database.');
$number++;
return $node;
}
}
Traits