]> BookStack Code Mirror - bookstack/blob - tests/Entity/PageTemplateTest.php
Create additional test helper classes
[bookstack] / tests / Entity / PageTemplateTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Page;
6 use Tests\TestCase;
7
8 class PageTemplateTest extends TestCase
9 {
10     public function test_active_templates_visible_on_page_view()
11     {
12         $page = $this->entities->page();
13
14         $this->asEditor();
15         $templateView = $this->get($page->getUrl());
16         $templateView->assertDontSee('Page Template');
17
18         $page->template = true;
19         $page->save();
20
21         $templateView = $this->get($page->getUrl());
22         $templateView->assertSee('Page Template');
23     }
24
25     public function test_manage_templates_permission_required_to_change_page_template_status()
26     {
27         $page = $this->entities->page();
28         $editor = $this->users->editor();
29         $this->actingAs($editor);
30
31         $pageUpdateData = [
32             'name'     => $page->name,
33             'html'     => $page->html,
34             'template' => 'true',
35         ];
36
37         $this->put($page->getUrl(), $pageUpdateData);
38         $this->assertDatabaseHas('pages', [
39             'id'       => $page->id,
40             'template' => false,
41         ]);
42
43         $this->permissions->grantUserRolePermissions($editor, ['templates-manage']);
44
45         $this->put($page->getUrl(), $pageUpdateData);
46         $this->assertDatabaseHas('pages', [
47             'id'       => $page->id,
48             'template' => true,
49         ]);
50     }
51
52     public function test_templates_content_should_be_fetchable_only_if_page_marked_as_template()
53     {
54         $content = '<div>my_custom_template_content</div>';
55         $page = $this->entities->page();
56         $editor = $this->users->editor();
57         $this->actingAs($editor);
58
59         $templateFetch = $this->get('/templates/' . $page->id);
60         $templateFetch->assertStatus(404);
61
62         $page->html = $content;
63         $page->template = true;
64         $page->save();
65
66         $templateFetch = $this->get('/templates/' . $page->id);
67         $templateFetch->assertStatus(200);
68         $templateFetch->assertJson([
69             'html'     => $content,
70             'markdown' => '',
71         ]);
72     }
73
74     public function test_template_endpoint_returns_paginated_list_of_templates()
75     {
76         $editor = $this->users->editor();
77         $this->actingAs($editor);
78
79         $toBeTemplates = Page::query()->orderBy('name', 'asc')->take(12)->get();
80         $page = $toBeTemplates->first();
81
82         $emptyTemplatesFetch = $this->get('/templates');
83         $emptyTemplatesFetch->assertDontSee($page->name);
84
85         Page::query()->whereIn('id', $toBeTemplates->pluck('id')->toArray())->update(['template' => true]);
86
87         $templatesFetch = $this->get('/templates');
88         $templatesFetch->assertSee($page->name);
89         $templatesFetch->assertSee('pagination');
90     }
91 }