-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathEntityProvider.php
More file actions
264 lines (233 loc) · 7.83 KB
/
Copy pathEntityProvider.php
File metadata and controls
264 lines (233 loc) · 7.83 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
<?php
namespace Tests\Helpers;
use BookStack\Entities\Models\Book;
use BookStack\Entities\Models\Bookshelf;
use BookStack\Entities\Models\Chapter;
use BookStack\Entities\Models\Entity;
use BookStack\Entities\Models\Page;
use BookStack\Entities\Repos\BookRepo;
use BookStack\Entities\Repos\BookshelfRepo;
use BookStack\Entities\Repos\ChapterRepo;
use BookStack\Entities\Repos\PageRepo;
use BookStack\Entities\Tools\TrashCan;
use BookStack\Users\Models\User;
use Illuminate\Database\Eloquent\Builder;
/**
* Class to provider and action entity models for common test case
* operations. Tracks handled models and only returns fresh models.
* Does not dedupe against nested/child/parent models.
*/
class EntityProvider
{
/**
* @var array<string, int[]>
*/
protected array $fetchCache = [
'book' => [],
'page' => [],
'bookshelf' => [],
'chapter' => [],
];
/**
* Get an unfetched page from the system.
*/
public function page(callable|null $queryFilter = null): Page
{
/** @var Page $page */
$page = Page::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['page'])->first();
$this->addToCache($page);
return $page;
}
public function pageWithinChapter(): Page
{
return $this->page(fn(Builder $query) => $query->whereHas('chapter')->with('chapter'));
}
public function pageNotWithinChapter(): Page
{
return $this->page(fn(Builder $query) => $query->whereNull('chapter_id'));
}
public function templatePage(): Page
{
$page = $this->page();
$page->template = true;
$page->save();
return $page;
}
/**
* Get an unfetched chapter from the system.
*/
public function chapter(callable|null $queryFilter = null): Chapter
{
/** @var Chapter $chapter */
$chapter = Chapter::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['chapter'])->first();
$this->addToCache($chapter);
return $chapter;
}
public function chapterHasPages(): Chapter
{
return $this->chapter(fn(Builder $query) => $query->whereHas('pages'));
}
/**
* Get an unfetched book from the system.
*/
public function book(callable|null $queryFilter = null): Book
{
/** @var Book $book */
$book = Book::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['book'])->first();
$this->addToCache($book);
return $book;
}
/**
* Get a book that has chapters and pages assigned.
*/
public function bookHasChaptersAndPages(): Book
{
return $this->book(function (Builder $query) {
$query->has('chapters')->has('pages')->with(['chapters', 'pages']);
});
}
/**
* Get an unfetched shelf from the system.
*/
public function shelf(callable|null $queryFilter = null): Bookshelf
{
/** @var Bookshelf $shelf */
$shelf = Bookshelf::query()->when($queryFilter, $queryFilter)->whereNotIn('id', $this->fetchCache['bookshelf'])->first();
$this->addToCache($shelf);
return $shelf;
}
/**
* Get a shelf that has books assigned.
*/
public function shelfHasBooks(): Bookshelf
{
return $this->shelf(fn(Builder $query) => $query->whereHas('books'));
}
/**
* Get all entity types from the system.
* @return array{page: Page, chapter: Chapter, book: Book, bookshelf: Bookshelf}
*/
public function all(): array
{
return [
'page' => $this->page(),
'chapter' => $this->chapter(),
'book' => $this->book(),
'bookshelf' => $this->shelf(),
];
}
public function updatePage(Page $page, array $data): Page
{
$this->addToCache($page);
return app()->make(PageRepo::class)->update($page, $data);
}
/**
* Create a book to page chain of entities that belong to a specific user.
* @return array{book: Book, chapter: Chapter, page: Page}
*/
public function createChainBelongingToUser(User $creatorUser, ?User $updaterUser = null): array
{
if (empty($updaterUser)) {
$updaterUser = $creatorUser;
}
$userAttrs = ['created_by' => $creatorUser->id, 'owned_by' => $creatorUser->id, 'updated_by' => $updaterUser->id];
/** @var Book $book */
$book = Book::factory()->create($userAttrs);
$chapter = Chapter::factory()->create(array_merge(['book_id' => $book->id], $userAttrs));
$page = Page::factory()->create(array_merge(['book_id' => $book->id, 'chapter_id' => $chapter->id], $userAttrs));
$book->rebuildPermissions();
$this->addToCache([$page, $chapter, $book]);
return compact('book', 'chapter', 'page');
}
/**
* Create and return a new bookshelf.
*/
public function newShelf(array $input = ['name' => 'test shelf', 'description' => 'My new test shelf']): Bookshelf
{
$shelf = app(BookshelfRepo::class)->create($input, []);
$this->addToCache($shelf);
return $shelf;
}
/**
* Create and return a new book.
*/
public function newBook(array $input = ['name' => 'test book', 'description' => 'My new test book']): Book
{
$book = app(BookRepo::class)->create($input);
$this->addToCache($book);
return $book;
}
/**
* Create and return a new test chapter.
*/
public function newChapter(array $input, Book $book): Chapter
{
$chapter = app(ChapterRepo::class)->create($input, $book);
$this->addToCache($chapter);
return $chapter;
}
/**
* Create and return a new test page.
*/
public function newPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page
{
$book = $this->book();
$pageRepo = app(PageRepo::class);
$draftPage = $pageRepo->getNewDraftPage($book);
$this->addToCache($draftPage);
return $pageRepo->publishDraft($draftPage, $input);
}
/**
* Create and return a new test draft page.
*/
public function newDraftPage(array $input = ['name' => 'test page', 'html' => 'My new test page']): Page
{
$book = $this->book();
$pageRepo = app(PageRepo::class);
$draftPage = $pageRepo->getNewDraftPage($book);
$pageRepo->updatePageDraft($draftPage, $input);
$this->addToCache($draftPage);
return $draftPage;
}
/**
* Send an entity to the recycle bin.
*/
public function sendToRecycleBin(Entity $entity)
{
$trash = app()->make(TrashCan::class);
if ($entity instanceof Page) {
$trash->softDestroyPage($entity);
} elseif ($entity instanceof Chapter) {
$trash->softDestroyChapter($entity);
} elseif ($entity instanceof Book) {
$trash->softDestroyBook($entity);
} elseif ($entity instanceof Bookshelf) {
$trash->softDestroyBookshelf($entity);
}
$entity->refresh();
if (is_null($entity->deleted_at)) {
throw new \Exception("Could not send entity type [{$entity->getMorphClass()}] to the recycle bin");
}
}
/**
* Fully destroy the given entity from the system, bypassing the recycle bin
* stage. Still runs through main app deletion logic.
*/
public function destroy(Entity $entity)
{
$trash = app()->make(TrashCan::class);
$trash->destroyEntity($entity);
}
/**
* @param Entity|Entity[] $entities
*/
protected function addToCache($entities): void
{
if (!is_array($entities)) {
$entities = [$entities];
}
foreach ($entities as $entity) {
$this->fetchCache[$entity->getType()][] = $entity->id;
}
}
}