View source
<?php
namespace Drupal\sitemap\Tests;
use Drupal\node\NodeInterface;
class SitemapBookTest extends SitemapBrowserTestBase {
use SitemapTestTrait;
public static $modules = [
'sitemap',
'book',
];
protected $book;
protected $nodes;
protected function setUp() {
parent::setUp();
$this->user = $this
->drupalCreateUser([
'administer sitemap',
'access sitemap',
'create book content',
'create new books',
'administer book outlines',
]);
$this
->drupalLogin($this->user);
$this->nodes = $this
->createBook();
}
public function testBooks() {
$this
->drupalGet('/sitemap');
$elements = $this
->cssSelect(".sitemap-plugin--book");
$this
->assertEquals(count($elements), 0, 'Books are not included.');
$bid = $this->book
->id();
$nodes = $this->nodes;
$edit = [
'plugins[book:' . $bid . '][enabled]' => TRUE,
];
$this
->saveSitemapForm($edit);
$this
->drupalGet('/sitemap');
$this
->assertSession()
->linkExists($this->book
->getTitle());
foreach ($nodes as $node) {
$this
->assertSession()
->linkExists($node
->getTitle());
}
$edit = [
'plugins[book:' . $bid . '][settings][show_expanded]' => FALSE,
];
$this
->saveSitemapForm($edit);
$this
->drupalGet('/sitemap');
$this
->assertSession()
->linkExists($this->book
->getTitle());
foreach ($nodes as $node) {
$this
->assertSession()
->linkNotExists($node
->getTitle());
}
}
public function testBooksCustomTitle() {
$bid = $this->book
->id();
$this
->saveSitemapForm([
'plugins[book:' . $bid . '][enabled]' => TRUE,
]);
$this
->titleTest($this->book
->label(), 'book', $bid, TRUE);
}
protected function createBook() {
$this->book = $this
->createBookNode('new');
$book = $this->book;
$nodes = [];
$nodes[] = $this
->createBookNode($book
->id());
$nodes[] = $this
->createBookNode($book
->id(), $nodes[0]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[0]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id());
$nodes[] = $this
->createBookNode($book
->id());
return $nodes;
}
protected function createBookNode($bid, $parent = NULL) {
$edit = [
'title[0][value]' => $this
->randomMachineName(10),
'book[bid]' => $bid,
];
if ($parent !== NULL) {
$this
->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
$edit['book[pid]'] = $parent;
$this
->drupalPostForm(NULL, $edit, t('Save'));
}
else {
$this
->drupalPostForm('node/add/book', $edit, t('Save'));
}
return $this
->drupalGetNodeByTitle($edit['title[0][value]']);
}
}