]> BookStack Code Mirror - bookstack/blob - tests/Entity/SlugTest.php
Slugs: Rolled out history lookup to other types
[bookstack] / tests / Entity / SlugTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use Tests\TestCase;
6
7 class SlugTest extends TestCase
8 {
9     public function test_slug_multi_byte_url_safe()
10     {
11         $book = $this->entities->newBook([
12             'name' => 'информация',
13         ]);
14
15         $this->assertEquals('informaciia', $book->slug);
16
17         $book = $this->entities->newBook([
18             'name' => '¿Qué?',
19         ]);
20
21         $this->assertEquals('que', $book->slug);
22     }
23
24     public function test_slug_format()
25     {
26         $book = $this->entities->newBook([
27             'name' => 'PartA / PartB / PartC',
28         ]);
29
30         $this->assertEquals('parta-partb-partc', $book->slug);
31     }
32
33     public function test_old_page_slugs_redirect_to_new_pages()
34     {
35         $page = $this->entities->page();
36         $pageUrl = $page->getUrl();
37
38         $this->asAdmin()->put($pageUrl, [
39             'name' => 'super test page',
40             'html' => '<p></p>',
41         ]);
42
43         $this->get($pageUrl)
44             ->assertRedirect("/books/{$page->book->slug}/page/super-test-page");
45     }
46
47     public function test_old_shelf_slugs_redirect_to_new_shelf()
48     {
49         $shelf = $this->entities->shelf();
50         $shelfUrl = $shelf->getUrl();
51
52         $this->asAdmin()->put($shelf->getUrl(), [
53             'name' => 'super test shelf',
54         ]);
55
56         $this->get($shelfUrl)
57             ->assertRedirect("/shelves/super-test-shelf");
58     }
59
60     public function test_old_book_slugs_redirect_to_new_book()
61     {
62         $book = $this->entities->book();
63         $bookUrl = $book->getUrl();
64
65         $this->asAdmin()->put($book->getUrl(), [
66             'name' => 'super test book',
67         ]);
68
69         $this->get($bookUrl)
70             ->assertRedirect("/books/super-test-book");
71     }
72
73     public function test_old_chapter_slugs_redirect_to_new_chapter()
74     {
75         $chapter = $this->entities->chapter();
76         $chapterUrl = $chapter->getUrl();
77
78         $this->asAdmin()->put($chapter->getUrl(), [
79             'name' => 'super test chapter',
80         ]);
81
82         $this->get($chapterUrl)
83             ->assertRedirect("/books/{$chapter->book->slug}/chapter/super-test-chapter");
84     }
85
86     public function test_old_book_slugs_in_page_urls_redirect_to_current_page_url()
87     {
88         $page = $this->entities->page();
89         $book = $page->book;
90         $pageUrl = $page->getUrl();
91
92         $this->asAdmin()->put($book->getUrl(), [
93             'name' => 'super test book',
94         ]);
95
96         $this->get($pageUrl)
97             ->assertRedirect("/books/super-test-book/page/{$page->slug}");
98     }
99
100     public function test_old_book_slugs_in_chapter_urls_redirect_to_current_chapter_url()
101     {
102         $chapter = $this->entities->chapter();
103         $book = $chapter->book;
104         $chapterUrl = $chapter->getUrl();
105
106         $this->asAdmin()->put($book->getUrl(), [
107             'name' => 'super test book',
108         ]);
109
110         $this->get($chapterUrl)
111             ->assertRedirect("/books/super-test-book/chapter/{$chapter->slug}");
112     }
113
114     public function test_slugs_recorded_in_history_on_page_update()
115     {
116         $page = $this->entities->page();
117         $this->asAdmin()->put($page->getUrl(), [
118             'name' => 'new slug',
119             'html' => '<p></p>',
120         ]);
121
122         $oldSlug = $page->slug;
123         $page->refresh();
124         $this->assertNotEquals($oldSlug, $page->slug);
125
126         $this->assertDatabaseHas('slug_history', [
127             'sluggable_id' => $page->id,
128             'sluggable_type' => 'page',
129             'slug' => $oldSlug,
130             'parent_slug' => $page->book->slug,
131         ]);
132     }
133
134     public function test_slugs_recorded_in_history_on_chapter_update()
135     {
136         $chapter = $this->entities->chapter();
137         $this->asAdmin()->put($chapter->getUrl(), [
138             'name' => 'new slug',
139         ]);
140
141         $oldSlug = $chapter->slug;
142         $chapter->refresh();
143         $this->assertNotEquals($oldSlug, $chapter->slug);
144
145         $this->assertDatabaseHas('slug_history', [
146             'sluggable_id' => $chapter->id,
147             'sluggable_type' => 'chapter',
148             'slug' => $oldSlug,
149             'parent_slug' => $chapter->book->slug,
150         ]);
151     }
152
153     public function test_slugs_recorded_in_history_on_book_update()
154     {
155         $book = $this->entities->book();
156         $this->asAdmin()->put($book->getUrl(), [
157             'name' => 'new slug',
158         ]);
159
160         $oldSlug = $book->slug;
161         $book->refresh();
162         $this->assertNotEquals($oldSlug, $book->slug);
163
164         $this->assertDatabaseHas('slug_history', [
165             'sluggable_id' => $book->id,
166             'sluggable_type' => 'book',
167             'slug' => $oldSlug,
168             'parent_slug' => null,
169         ]);
170     }
171
172     public function test_slugs_recorded_in_history_on_shelf_update()
173     {
174         $shelf = $this->entities->shelf();
175         $this->asAdmin()->put($shelf->getUrl(), [
176             'name' => 'new slug',
177         ]);
178
179         $oldSlug = $shelf->slug;
180         $shelf->refresh();
181         $this->assertNotEquals($oldSlug, $shelf->slug);
182
183         $this->assertDatabaseHas('slug_history', [
184             'sluggable_id' => $shelf->id,
185             'sluggable_type' => 'bookshelf',
186             'slug' => $oldSlug,
187             'parent_slug' => null,
188         ]);
189     }
190 }