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