]> BookStack Code Mirror - bookstack/commitdiff
Made custom home ignore permissions and added tests
authorDan Brown <redacted>
Mon, 28 Aug 2017 12:55:39 +0000 (13:55 +0100)
committerDan Brown <redacted>
Mon, 28 Aug 2017 12:55:39 +0000 (13:55 +0100)
Closes #126 and #372

app/Http/Controllers/HomeController.php
app/Repos/EntityRepo.php
tests/HomepageTest.php [new file with mode: 0644]
tests/TestCase.php

index e9cd72fe219a0f64b04157e27c3048c11f96375d..7d109b70a9bb9225e2d8edfccc678e191765acbf 100644 (file)
@@ -37,8 +37,8 @@ class HomeController extends Controller
         $homepageSetting = setting('app-homepage');
         if ($homepageSetting) {
             $id = intval(explode(':', $homepageSetting)[0]);
-            $customHomepage = $this->entityRepo->getById('page', $id);
-            $this->entityRepo->renderPage($customHomepage);
+            $customHomepage = $this->entityRepo->getById('page', $id, false, true);
+            $this->entityRepo->renderPage($customHomepage, true);
         }
 
         $view = $customHomepage ? 'home-custom' : 'home';
index b2067ad7b75a1f6c661cf0c26ef8f086c5c38feb..a682e696bae5d1c4e633d649ef260433480e8426 100644 (file)
@@ -137,10 +137,15 @@ class EntityRepo
      * @param string $type
      * @param integer $id
      * @param bool $allowDrafts
+     * @param bool $ignorePermissions
      * @return Entity
      */
-    public function getById($type, $id, $allowDrafts = false)
+    public function getById($type, $id, $allowDrafts = false, $ignorePermissions = false)
     {
+        if ($ignorePermissions) {
+            $entity = $this->getEntity($type);
+            return $entity->newQuery()->find($id);
+        }
         return $this->entityQuery($type, $allowDrafts)->find($id);
     }
 
@@ -671,9 +676,10 @@ class EntityRepo
     /**
      * Render the page for viewing, Parsing and performing features such as page transclusion.
      * @param Page $page
+     * @param bool $ignorePermissions
      * @return mixed|string
      */
-    public function renderPage(Page $page)
+    public function renderPage(Page $page, $ignorePermissions = false)
     {
         $content = $page->html;
         $matches = [];
@@ -685,7 +691,7 @@ class EntityRepo
             $pageId = intval($splitInclude[0]);
             if (is_nan($pageId)) continue;
 
-            $page = $this->getById('page', $pageId);
+            $page = $this->getById('page', $pageId, false, $ignorePermissions);
             if ($page === null) {
                 $content = str_replace($matches[0][$index], '', $content);
                 continue;
diff --git a/tests/HomepageTest.php b/tests/HomepageTest.php
new file mode 100644 (file)
index 0000000..7c77e94
--- /dev/null
@@ -0,0 +1,33 @@
+<?php namespace Tests;
+
+use BookStack\JointPermission;
+use BookStack\Page;
+use BookStack\Repos\EntityRepo;
+
+class HomepageTest extends TestCase
+{
+
+    public function test_default_homepage_visible()
+    {
+        $this->asEditor();
+        $homeVisit = $this->get('/');
+        $homeVisit->assertSee('My Recently Viewed');
+        $homeVisit->assertSee('Recently Updated Pages');
+        $homeVisit->assertSee('Recent Activity');
+    }
+
+    public function test_custom_homepage() {
+        $this->asEditor();
+        $name = 'My custom homepage';
+        $content = 'This is the body content of my custom homepage.';
+        $customPage = $this->newPage(['name' => $name, 'html' => $content]);
+        $this->setSettings(['app-homepage' => $customPage->id]);
+
+        $homeVisit = $this->get('/');
+        $homeVisit->assertSee($name);
+        $homeVisit->assertSee($content);
+        $homeVisit->assertSee('My Recently Viewed');
+        $homeVisit->assertSee('Recently Updated Pages');
+        $homeVisit->assertSee('Recent Activity');
+    }
+}
index b008080d9c3ed92cad3976e6c554628facbb48a3..81bd93ec4de41fdea805824cf22980a5ea785531 100644 (file)
@@ -4,6 +4,7 @@ use BookStack\Book;
 use BookStack\Chapter;
 use BookStack\Repos\EntityRepo;
 use BookStack\Role;
+use BookStack\Services\SettingService;
 use Illuminate\Foundation\Testing\DatabaseTransactions;
 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
 
@@ -88,4 +89,16 @@ abstract class TestCase extends BaseTestCase
         $draftPage = $entityRepo->getDraftPage($book);
         return $entityRepo->publishPageDraft($draftPage, $input);
     }
+
+    /**
+     * Quickly sets an array of settings.
+     * @param $settingsArray
+     */
+    protected function setSettings($settingsArray)
+    {
+        $settings = app(SettingService::class);
+        foreach ($settingsArray as $key => $value) {
+            $settings->put($key, $value);
+        }
+    }
 }
\ No newline at end of file