Backup of db before drupal security update
[yaffs-website] / web / core / modules / book / src / Tests / Views / BookRelationshipTest.php
1 <?php
2
3 namespace Drupal\book\Tests\Views;
4
5 use Drupal\views\Tests\ViewTestBase;
6 use Drupal\views\Tests\ViewTestData;
7
8 /**
9  * Tests entity reference relationship data.
10  *
11  * @group book
12  *
13  * @see book_views_data()
14  */
15 class BookRelationshipTest extends ViewTestBase {
16
17   /**
18    * Views used by this test.
19    *
20    * @var array
21    */
22   public static $testViews = ['test_book_view'];
23
24   /**
25    * Modules to install.
26    *
27    * @var array
28    */
29   public static $modules = ['book_test_views', 'book', 'views'];
30
31   /**
32    * A book node.
33    *
34    * @var object
35    */
36   protected $book;
37
38   /**
39    * A user with permission to create and edit books.
40    *
41    * @var object
42    */
43   protected $bookAuthor;
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUp() {
49     parent::setUp();
50
51     // Create users.
52     $this->bookAuthor = $this->drupalCreateUser(
53       [
54         'create new books',
55         'create book content',
56         'edit own book content',
57         'add content to books',
58       ]
59     );
60     ViewTestData::createTestViews(get_class($this), ['book_test_views']);
61   }
62
63   /**
64    * Creates a new book with a page hierarchy.
65    */
66   protected function createBook() {
67     // Create new book.
68     $this->drupalLogin($this->bookAuthor);
69
70     $this->book = $this->createBookNode('new');
71     $book = $this->book;
72
73     $nodes = [];
74     // Node 0.
75     $nodes[] = $this->createBookNode($book->id());
76     // Node 1.
77     $nodes[] = $this->createBookNode($book->id(), $nodes[0]->book['nid']);
78     // Node 2.
79     $nodes[] = $this->createBookNode($book->id(), $nodes[1]->book['nid']);
80     // Node 3.
81     $nodes[] = $this->createBookNode($book->id(), $nodes[2]->book['nid']);
82     // Node 4.
83     $nodes[] = $this->createBookNode($book->id(), $nodes[3]->book['nid']);
84     // Node 5.
85     $nodes[] = $this->createBookNode($book->id(), $nodes[4]->book['nid']);
86     // Node 6.
87     $nodes[] = $this->createBookNode($book->id(), $nodes[5]->book['nid']);
88     // Node 7.
89     $nodes[] = $this->createBookNode($book->id(), $nodes[6]->book['nid']);
90
91     $this->drupalLogout();
92
93     return $nodes;
94   }
95
96   /**
97    * Creates a book node.
98    *
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.
103    *
104    * @return \Drupal\node\NodeInterface
105    *   The book node.
106    */
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.
111     static $number = 0;
112
113     $edit = [];
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;
117
118     if ($parent !== NULL) {
119       $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
120
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');
126     }
127     else {
128       $this->drupalPostForm('node/add/book', $edit, t('Save'));
129     }
130
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.');
134     $number++;
135
136     return $node;
137   }
138
139   /**
140    * Tests using the views relationship.
141    */
142   public function testRelationship() {
143
144     // Create new book.
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());
149
150       for ($j = 0; $j < $i; $j++) {
151         $this->assertLink($nodes[$j]->label());
152       }
153     }
154   }
155
156 }