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