]> BookStack Code Mirror - bookstack/blob - database/seeders/DummyContentSeeder.php
Copying: Fixed issue with non-page links to page permalinks
[bookstack] / database / seeders / DummyContentSeeder.php
1 <?php
2
3 namespace Database\Seeders;
4
5 use BookStack\Api\ApiToken;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Chapter;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Permissions\JointPermissionBuilder;
11 use BookStack\Permissions\Models\RolePermission;
12 use BookStack\Search\SearchIndex;
13 use BookStack\Users\Models\Role;
14 use BookStack\Users\Models\User;
15 use Illuminate\Database\Eloquent\Relations\HasMany;
16 use Illuminate\Database\Seeder;
17 use Illuminate\Support\Collection;
18 use Illuminate\Support\Facades\Hash;
19 use Illuminate\Support\Str;
20
21 class DummyContentSeeder extends Seeder
22 {
23     /**
24      * Run the database seeds.
25      */
26     public function run(): void
27     {
28         // Create an editor user
29         $editorUser = User::factory()->create();
30         $editorRole = Role::getRole('editor');
31         $additionalEditorPerms = ['receive-notifications', 'comment-create-all'];
32         $editorRole->permissions()->syncWithoutDetaching(RolePermission::whereIn('name', $additionalEditorPerms)->pluck('id'));
33         $editorUser->attachRole($editorRole);
34
35         // Create a viewer user
36         $viewerUser = User::factory()->create();
37         $role = Role::getRole('viewer');
38         $viewerUser->attachRole($role);
39
40         $byData = ['created_by' => $editorUser->id, 'updated_by' => $editorUser->id, 'owned_by' => $editorUser->id];
41
42         Book::factory()->count(5)->make($byData)
43             ->each(function ($book) use ($byData) {
44                 $book->save();
45                 $chapters = Chapter::factory()->count(3)->create($byData)
46                     ->each(function ($chapter) use ($book, $byData) {
47                         $pages = Page::factory()->count(3)->make(array_merge($byData, ['book_id' => $book->id]));
48                         $this->saveManyOnRelation($pages, $chapter->pages());
49                     });
50                 $pages = Page::factory()->count(3)->make($byData);
51                 $this->saveManyOnRelation($chapters, $book->chapters());
52                 $this->saveManyOnRelation($pages, $book->pages());
53             });
54
55         $largeBook = Book::factory()->make(array_merge($byData, ['name' => 'Large book' . Str::random(10)]));
56         $largeBook->save();
57
58         $pages = Page::factory()->count(200)->make($byData);
59         $chapters = Chapter::factory()->count(50)->make($byData);
60         $this->saveManyOnRelation($pages, $largeBook->pages());
61         $this->saveManyOnRelation($chapters, $largeBook->chapters());
62
63         $shelves = Bookshelf::factory()->count(10)->make($byData);
64         foreach ($shelves as $shelf) {
65             $shelf->save();
66         }
67
68         $largeBook->shelves()->attach($shelves->pluck('id'));
69
70         // Assign API permission to editor role and create an API key
71         $apiPermission = RolePermission::getByName('access-api');
72         $editorRole->attachPermission($apiPermission);
73         $token = (new ApiToken())->forceFill([
74             'user_id' => $editorUser->id,
75             'name' => 'Testing API key',
76             'expires_at' => ApiToken::defaultExpiry(),
77             'secret' => Hash::make('password'),
78             'token_id' => 'apitoken',
79         ]);
80         $token->save();
81
82         app(JointPermissionBuilder::class)->rebuildForAll();
83         app(SearchIndex::class)->indexAllEntities();
84     }
85
86     /**
87      * Inefficient workaround for saving many on a relation since we can't directly insert
88      * entities since we split them across tables.
89      */
90     protected function saveManyOnRelation(Collection $entities, HasMany $relation): void
91     {
92         foreach ($entities as $entity) {
93             $relation->save($entity);
94         }
95     }
96 }