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