Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / BookTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\node\Entity\Node;
6 use Drupal\Core\Url;
7
8 /**
9  * Test the book tokens.
10  *
11  * @group token
12  */
13 class BookTest extends KernelTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['user', 'field', 'filter', 'text', 'node', 'book'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27
28     $this->installEntitySchema('user');
29     $this->installEntitySchema('node');
30     $this->installSchema('book', ['book']);
31     $this->installSchema('node', ['node_access']);
32     $this->installConfig(['node', 'book', 'field']);
33   }
34
35   function testBookTokens() {
36     $book = Node::create([
37       'type' => 'book',
38       'title' => 'Book Main Page',
39       'book' => ['bid' => 'new'],
40     ]);
41     $book->save();
42
43     $page1 = Node::create([
44       'type' => 'book',
45       'title' => '1st Page',
46       'book' => ['bid' => $book->id(), 'pid' => $book->id()],
47     ]);
48     $page1->save();
49
50     $page2 = Node::create([
51       'type' => 'book',
52       'title' => '2nd Page',
53       'book' => ['bid' => $book->id(), 'pid' => $page1->id()],
54     ]);
55     $page2->save();
56
57     $book_title = $book->getTitle();
58
59     $tokens = [
60       'nid' => $book->id(),
61       'title' => $book_title,
62       'book:title' => $book_title,
63       'book:root' => $book_title,
64       'book:root:nid' => $book->id(),
65       'book:root:title' => $book_title,
66       'book:root:url' => Url::fromRoute('entity.node.canonical', ['node' => $book->id()], ['absolute' => TRUE])->toString(),
67       'book:root:content-type' => 'Book page',
68       'book:parent' => null,
69       'book:parents' => null,
70     ];
71     $this->assertTokens('node', ['node' => $book], $tokens);
72
73     $tokens = [
74       'nid' => $page1->id(),
75       'title' => $page1->getTitle(),
76       'book:title' => $book_title,
77       'book:root' => $book_title,
78       'book:root:nid' => $book->id(),
79       'book:root:title' => $book_title,
80       'book:root:url' => Url::fromRoute('entity.node.canonical', ['node' => $book->id()], ['absolute' => TRUE])->toString(),
81       'book:root:content-type' => 'Book page',
82       'book:parent:nid' => $book->id(),
83       'book:parent:title' => $book_title,
84       'book:parent:url' => Url::fromRoute('entity.node.canonical', ['node' => $book->id()], ['absolute' => TRUE])->toString(),
85       'book:parents:count' => 1,
86       'book:parents:join:/' => $book_title,
87     ];
88     $this->assertTokens('node', ['node' => $page1], $tokens);
89
90     $tokens = [
91       'nid' => $page2->id(),
92       'title' => $page2->getTitle(),
93       'book:title' => $book_title,
94       'book:root' => $book_title,
95       'book:root:nid' => $book->id(),
96       'book:root:title' => $book_title,
97       'book:root:url' => Url::fromRoute('entity.node.canonical', ['node' => $book->id()], ['absolute' => TRUE])->toString(),
98       'book:root:content-type' => 'Book page',
99       'book:parent:nid' => $page1->id(),
100       'book:parent:title' => $page1->getTitle(),
101       'book:parent:url' => Url::fromRoute('entity.node.canonical', ['node' => $page1->id()], ['absolute' => TRUE])->toString(),
102       'book:parents:count' => 2,
103       'book:parents:join:/' => $book_title . '/' . $page1->getTitle(),
104     ];
105     $this->assertTokens('node', ['node' => $page2], $tokens);
106   }
107
108 }