View source
<?php
namespace Drupal\sitemap\Tests;
use Drupal\simpletest\WebTestBase;
class SitemapBookTest extends WebTestBase {
public static $modules = [
'sitemap',
'book',
];
protected $book;
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);
}
public function testBooks() {
$this
->drupalGet('/sitemap');
$elements = $this
->cssSelect(".sitemap-box h2:contains('Books')");
$this
->assertEqual(count($elements), 0, 'Books are not included.');
$nodes = $this
->createBook();
$book = $this->book;
$bid = $book
->id();
$edit = [
'show_books[' . $bid . ']' => $bid,
];
$this
->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration'));
$this
->drupalGet('/sitemap');
$this
->assertLink($this->book
->getTitle());
foreach ($nodes as $node) {
$this
->assertLink($node
->getTitle());
}
$edit = [
'books_expanded' => FALSE,
];
$this
->drupalPostForm('admin/config/search/sitemap', $edit, t('Save configuration'));
$this
->drupalGet('/sitemap');
$this
->assertLink($this->book
->getTitle());
foreach ($nodes as $node) {
$this
->assertNoLink($node
->getTitle());
}
}
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($book_nid, $parent = NULL) {
static $number = 0;
$edit = [];
$edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this
->randomMachineName(10);
$edit['body[0][value]'] = 'SimpleTest test body ' . $this
->randomMachineName(32) . ' ' . $this
->randomMachineName(32);
$edit['book[bid]'] = $book_nid;
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'));
}
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$number++;
return $node;
}
}