View source
<?php
namespace Drupal\Tests\book\Functional\Views;
use Drupal\Tests\views\Functional\ViewTestBase;
use Drupal\views\Tests\ViewTestData;
class BookRelationshipTest extends ViewTestBase {
public static $testViews = [
'test_book_view',
];
public static $modules = [
'book_test_views',
'book',
'views',
];
protected $defaultTheme = 'stark';
protected $book;
protected $bookAuthor;
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->bookAuthor = $this
->drupalCreateUser([
'create new books',
'create book content',
'edit own book content',
'add content to books',
]);
ViewTestData::createTestViews(get_class($this), [
'book_test_views',
]);
}
protected function createBook() {
$this
->drupalLogin($this->bookAuthor);
$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[1]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[2]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[3]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[4]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[5]->book['nid']);
$nodes[] = $this
->createBookNode($book
->id(), $nodes[6]->book['nid']);
$this
->drupalLogout();
return $nodes;
}
protected function createBookNode($book_nid, $parent = NULL) {
static $number = 0;
$edit = [];
$edit['title[0][value]'] = $number . ' - 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'));
$parent_node = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($parent);
$this
->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
}
else {
$this
->drupalPostForm('node/add/book', $edit, t('Save'));
}
$node = $this
->drupalGetNodeByTitle($edit['title[0][value]']);
$this
->assertNotNull($node === FALSE ? NULL : $node, 'Book node found in database.');
$number++;
return $node;
}
public function testRelationship() {
$nodes = $this
->createBook();
for ($i = 0; $i < 8; $i++) {
$this
->drupalGet('test-book/' . $nodes[$i]
->id());
for ($j = 0; $j < $i; $j++) {
$this
->assertSession()
->linkExists($nodes[$j]
->label());
}
}
}
}