View source
<?php
namespace Drupal\Tests\book\Functional;
use Drupal\Tests\BrowserTestBase;
class BookBreadcrumbTest extends BrowserTestBase {
protected static $modules = [
'book',
'block',
'book_breadcrumb_test',
];
protected $defaultTheme = 'stark';
protected $book;
protected $bookAuthor;
protected $webUserWithoutNodeAccess;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('system_breadcrumb_block');
$this
->drupalPlaceBlock('page_title_block');
$this->bookAuthor = $this
->drupalCreateUser([
'create new books',
'create book content',
'edit own book content',
'add content to books',
]);
}
protected function createBreadcrumbBook() {
$this
->drupalLogin($this->bookAuthor);
$this->book = $this
->createBookNode('new');
$book = $this->book;
$nodes = [];
$nodes[0] = $this
->createBookNode($book
->id());
$nodes[1] = $this
->createBookNode($book
->id(), $nodes[0]
->id());
$nodes[2] = $this
->createBookNode($book
->id(), $nodes[0]
->id());
$nodes[3] = $this
->createBookNode($book
->id(), $nodes[2]
->id());
$nodes[4] = $this
->createBookNode($book
->id(), $nodes[3]
->id());
$nodes[5] = $this
->createBookNode($book
->id(), $nodes[4]
->id());
$nodes[6] = $this
->createBookNode($book
->id());
$this
->drupalLogout();
return $nodes;
}
protected function createBookNode($book_nid, $parent = NULL) {
static $number = 0;
$edit = [];
$edit['title[0][value]'] = str_pad($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;
}
public function testBreadcrumbTitleUpdates() {
$nodes = $this
->createBreadcrumbBook();
$book = $this->book;
$this
->drupalLogin($this->bookAuthor);
$this
->drupalGet($nodes[4]
->toUrl());
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link
->getText();
}
$this
->assertCount(5, $got_breadcrumb);
$this
->assertEquals($nodes[3]
->getTitle(), end($got_breadcrumb));
$edit = [
'title[0][value]' => 'Updated node5 title',
];
$this
->drupalGet($nodes[3]
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->drupalGet($nodes[4]
->toUrl());
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link
->getText();
}
$this
->assertCount(5, $got_breadcrumb);
$this
->assertEquals($edit['title[0][value]'], end($got_breadcrumb));
}
public function testBreadcrumbAccessUpdates() {
$nodes = $this
->createBreadcrumbBook();
$this
->drupalLogin($this->bookAuthor);
$edit = [
'title[0][value]' => "you can't see me",
];
$this
->drupalGet($nodes[3]
->toUrl('edit-form'));
$this
->submitForm($edit, 'Save');
$this
->drupalGet($nodes[4]
->toUrl());
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link
->getText();
}
$this
->assertCount(5, $got_breadcrumb);
$this
->assertEquals($edit['title[0][value]'], end($got_breadcrumb));
$config = $this->container
->get('config.factory')
->getEditable('book_breadcrumb_test.settings');
$config
->set('hide', TRUE)
->save();
$this
->drupalGet($nodes[4]
->toUrl());
$links = $this
->xpath('//nav[@aria-labelledby="system-breadcrumb"]/ol/li/a');
$got_breadcrumb = [];
foreach ($links as $link) {
$got_breadcrumb[] = $link
->getText();
}
$this
->assertCount(4, $got_breadcrumb);
$this
->assertEquals($nodes[2]
->getTitle(), end($got_breadcrumb));
$this
->drupalGet($nodes[3]
->toUrl());
$this
->assertSession()
->statusCodeEquals(403);
}
}