Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / book / tests / src / Functional / BookBreadcrumbTest.php
1 <?php
2
3 namespace Drupal\Tests\book\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Create a book, add pages, and test book interface.
9  *
10  * @group book
11  */
12 class BookBreadcrumbTest extends BrowserTestBase {
13
14   /**
15    * Modules to install.
16    *
17    * @var array
18    */
19   public static $modules = ['book', 'block', 'book_breadcrumb_test'];
20
21   /**
22    * A book node.
23    *
24    * @var \Drupal\node\NodeInterface
25    */
26   protected $book;
27
28   /**
29    * A user with permission to create and edit books.
30    *
31    * @var \Drupal\user\Entity\User
32    */
33   protected $bookAuthor;
34
35   /**
36    * A user without the 'node test view' permission.
37    *
38    * @var \Drupal\user\UserInterface
39    */
40   protected $webUserWithoutNodeAccess;
41
42   /**
43    * {@inheritdoc}
44    */
45   protected function setUp() {
46     parent::setUp();
47     $this->drupalPlaceBlock('system_breadcrumb_block');
48     $this->drupalPlaceBlock('page_title_block');
49
50     // Create users.
51     $this->bookAuthor = $this->drupalCreateUser(['create new books', 'create book content', 'edit own book content', 'add content to books']);
52     $this->adminUser = $this->drupalCreateUser(['create new books', 'create book content', 'edit any book content', 'delete any book content', 'add content to books', 'administer blocks', 'administer permissions', 'administer book outlines', 'administer content types', 'administer site configuration']);
53   }
54
55   /**
56    * Creates a new book with a page hierarchy.
57    *
58    * @return \Drupal\node\NodeInterface[]
59    *   The created book nodes.
60    */
61   protected function createBreadcrumbBook() {
62     // Create new book.
63     $this->drupalLogin($this->bookAuthor);
64
65     $this->book = $this->createBookNode('new');
66     $book = $this->book;
67
68     /*
69      * Add page hierarchy to book.
70      * Book
71      *  |- Node 0
72      *   |- Node 1
73      *   |- Node 2
74      *    |- Node 3
75      *     |- Node 4
76      *      |- Node 5
77      *  |- Node 6
78      */
79     $nodes = [];
80     $nodes[0] = $this->createBookNode($book->id());
81     $nodes[1] = $this->createBookNode($book->id(), $nodes[0]->id());
82     $nodes[2] = $this->createBookNode($book->id(), $nodes[0]->id());
83     $nodes[3] = $this->createBookNode($book->id(), $nodes[2]->id());
84     $nodes[4] = $this->createBookNode($book->id(), $nodes[3]->id());
85     $nodes[5] = $this->createBookNode($book->id(), $nodes[4]->id());
86     $nodes[6] = $this->createBookNode($book->id());
87
88     $this->drupalLogout();
89
90     return $nodes;
91   }
92
93   /**
94    * Creates a book node.
95    *
96    * @param int|string $book_nid
97    *   A book node ID or set to 'new' to create a new book.
98    * @param int|null $parent
99    *   (optional) Parent book reference ID. Defaults to NULL.
100    *
101    * @return \Drupal\node\NodeInterface
102    *   The created node.
103    */
104   protected function createBookNode($book_nid, $parent = NULL) {
105     // $number does not use drupal_static as it should not be reset since it
106     // uniquely identifies each call to createBookNode(). It is used to ensure
107     // that when sorted nodes stay in same order.
108     static $number = 0;
109
110     $edit = [];
111     $edit['title[0][value]'] = str_pad($number, 2, '0', STR_PAD_LEFT) . ' - SimpleTest test node ' . $this->randomMachineName(10);
112     $edit['body[0][value]'] = 'SimpleTest test body ' . $this->randomMachineName(32) . ' ' . $this->randomMachineName(32);
113     $edit['book[bid]'] = $book_nid;
114
115     if ($parent !== NULL) {
116       $this->drupalPostForm('node/add/book', $edit, t('Change book (update list of parents)'));
117
118       $edit['book[pid]'] = $parent;
119       $this->drupalPostForm(NULL, $edit, t('Save'));
120       // Make sure the parent was flagged as having children.
121       $parent_node = \Drupal::entityManager()->getStorage('node')->loadUnchanged($parent);
122       $this->assertFalse(empty($parent_node->book['has_children']), 'Parent node is marked as having children');
123     }
124     else {
125       $this->drupalPostForm('node/add/book', $edit, t('Save'));
126     }
127
128     // Check to make sure the book node was created.
129     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
130     $this->assertNotNull(($node === FALSE ? NULL : $node), 'Book node found in database.');
131     $number++;
132
133     return $node;
134   }
135
136   /**
137    * Test that the breadcrumb is updated when book content changes.
138    */
139   public function testBreadcrumbTitleUpdates() {
140     // Create a new book.
141     $nodes = $this->createBreadcrumbBook();
142     $book = $this->book;
143
144     $this->drupalLogin($this->bookAuthor);
145
146     $this->drupalGet($nodes[4]->toUrl());
147     // Fetch each node title in the current breadcrumb.
148     $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
149     $got_breadcrumb = [];
150     foreach ($links as $link) {
151       $got_breadcrumb[] = $link->getText();
152     }
153     // Home link and four parent book nodes should be in the breadcrumb.
154     $this->assertEqual(5, count($got_breadcrumb));
155     $this->assertEqual($nodes[3]->getTitle(), end($got_breadcrumb));
156     $edit = [
157       'title[0][value]' => 'Updated node5 title',
158     ];
159     $this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
160     $this->drupalGet($nodes[4]->toUrl());
161     // Fetch each node title in the current breadcrumb.
162     $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
163     $got_breadcrumb = [];
164     foreach ($links as $link) {
165       $got_breadcrumb[] = $link->getText();
166     }
167     $this->assertEqual(5, count($got_breadcrumb));
168     $this->assertEqual($edit['title[0][value]'], end($got_breadcrumb));
169   }
170
171   /**
172    * Test that the breadcrumb is updated when book access changes.
173    */
174   public function testBreadcrumbAccessUpdates() {
175     // Create a new book.
176     $nodes = $this->createBreadcrumbBook();
177     $this->drupalLogin($this->bookAuthor);
178     $edit = [
179       'title[0][value]' => "you can't see me",
180     ];
181     $this->drupalPostForm($nodes[3]->toUrl('edit-form'), $edit, 'Save');
182     $this->drupalGet($nodes[4]->toUrl());
183     $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
184     $got_breadcrumb = [];
185     foreach ($links as $link) {
186       $got_breadcrumb[] = $link->getText();
187     }
188     $this->assertEqual(5, count($got_breadcrumb));
189     $this->assertEqual($edit['title[0][value]'], end($got_breadcrumb));
190     $config = $this->container->get('config.factory')->getEditable('book_breadcrumb_test.settings');
191     $config->set('hide', TRUE)->save();
192     $this->drupalGet($nodes[4]->toUrl());
193     $links = $this->xpath('//nav[@class="breadcrumb"]/ol/li/a');
194     $got_breadcrumb = [];
195     foreach ($links as $link) {
196       $got_breadcrumb[] = $link->getText();
197     }
198     $this->assertEqual(4, count($got_breadcrumb));
199     $this->assertEqual($nodes[2]->getTitle(), end($got_breadcrumb));
200     $this->drupalGet($nodes[3]->toUrl());
201     $this->assertResponse(403);
202   }
203
204 }