3 namespace Drupal\book\Tests\Views;
5 use Drupal\views\Tests\ViewTestBase;
6 use Drupal\views\Tests\ViewTestData;
9 * Tests entity reference relationship data.
13 * @see book_views_data()
15 class BookRelationshipTest extends ViewTestBase {
18 * Views used by this test.
22 public static $testViews = ['test_book_view'];
29 public static $modules = ['book_test_views', 'book', 'views'];
39 * A user with permission to create and edit books.
43 protected $bookAuthor;
48 protected function setUp() {
52 $this->bookAuthor = $this->drupalCreateUser(
55 'create book content',
56 'edit own book content',
57 'add content to books',
60 ViewTestData::createTestViews(get_class($this), ['book_test_views']);
64 * Creates a new book with a page hierarchy.
66 protected function createBook() {
68 $this->drupalLogin($this->bookAuthor);
70 $this->book = $this->createBookNode('new');
75 $nodes[] = $this->createBookNode($book->id());
77 $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid']);
79 $nodes[] = $this->createBookNode($book->id(), $nodes[1]->book['nid']);
81 $nodes[] = $this->createBookNode($book->id(), $nodes[2]->book['nid']);
83 $nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']);
85 $nodes[] = $this->createBookNode($book->id(), $nodes[4]->book['nid']);
87 $nodes[] = $this->createBookNode($book->id(), $nodes[5]->book['nid']);
89 $nodes[] = $this->createBookNode($book->id(), $nodes[6]->book['nid']);
91 $this->drupalLogout();
97 * Creates a book node.
99 * @param int|string $book_nid
100 * A book node ID or set to 'new' to create a new book.
101 * @param int|null $parent
102 * (optional) Parent book reference ID. Defaults to NULL.
104 * @return \Drupal\node\NodeInterface
107 protected function createBookNode($book_nid, $parent = NULL) {
108 // $number does not use drupal_static as it should not be reset
109 // since it uniquely identifies each call to createBookNode().
110 // Used to ensure that when sorted nodes stay in same order.
114 $edit['title[0][value]'] = $number . ' - SimpleTest test node ' . $this->randomMachineName(10);
115 $edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
116 $edit['book[bid]'] = $book_nid;
118 if ($parent !== NULL) {
119 $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
121 $edit['book[pid]'] = $parent;
122 $this->drupalPostForm(NULL, $edit, t('Save'));
123 // Make sure the parent was flagged as having children.
124 $parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
125 $this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
128 $this->drupalPostForm('node/add/book', $edit, t('Save'));
131 // Check to make sure the book node was created.
132 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
133 $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
140 * Tests using the views relationship.
142 public function testRelationship() {
145 // @var \Drupal\node\NodeInterface[] $nodes
146 $nodes = $this->createBook();
147 for ($i = 0; $i < 8; $i++) {
148 $this->drupalGet('test-book/' . $nodes[$i]->id());
150 for ($j = 0; $j < $i; $j++) {
151 $this->assertLink($nodes[$j]->label());