]> BookStack Code Mirror - bookstack/blobdiff - tests/EntityTest.php
Update PageRepo.php
[bookstack] / tests / EntityTest.php
index ddbed731e4e401793b7854d32bdc5adad6c5c4d7..5bfedb53561952cde7d362e1d03e044e70cb926c 100644 (file)
@@ -1,9 +1,11 @@
 <?php
 
+use Illuminate\Support\Facades\DB;
+
 class EntityTest extends TestCase
 {
 
-    public function testEntityCreation()
+    public function test_entity_creation()
     {
 
         // Test Creation
@@ -49,6 +51,34 @@ class EntityTest extends TestCase
         return \BookStack\Book::find($book->id);
     }
 
+    public function test_book_sort_page_shows()
+    {
+        $books =  \BookStack\Book::all();
+        $bookToSort = $books[0];
+        $this->asAdmin()
+            ->visit($bookToSort->getUrl())
+            ->click('Sort')
+            ->seePageIs($bookToSort->getUrl() . '/sort')
+            ->seeStatusCode(200)
+            ->see($bookToSort->name)
+            // Ensure page shows other books
+            ->see($books[1]->name);
+    }
+
+    public function test_book_sort_item_returns_book_content()
+    {
+        $books =  \BookStack\Book::all();
+        $bookToSort = $books[0];
+        $firstPage = $bookToSort->pages[0];
+        $firstChapter = $bookToSort->chapters[0];
+        $this->asAdmin()
+            ->visit($bookToSort->getUrl() . '/sort-item')
+            // Ensure book details are returned
+            ->see($bookToSort->name)
+            ->see($firstPage->name)
+            ->see($firstChapter->name);
+    }
+
     public function pageCreation($chapter)
     {
         $page = factory(\BookStack\Page::class)->make([
@@ -113,9 +143,112 @@ class EntityTest extends TestCase
             ->seePageIs('/books/my-first-book')
             ->see($book->name)->see($book->description);
 
+        // Ensure duplicate names are given different slugs
+        $this->asAdmin()
+            ->visit('/books/create')
+            ->type($book->name, '#name')
+            ->type($book->description, '#description')
+            ->press('Save Book')
+            ->seePageIs('/books/my-first-book-2');
+
         $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
         return $book;
     }
 
+    public function test_page_search()
+    {
+        $book = \BookStack\Book::all()->first();
+        $page = $book->pages->first();
+
+        $this->asAdmin()
+            ->visit('/')
+            ->type($page->name, 'term')
+            ->press('header-search-box-button')
+            ->see('Search Results')
+            ->see($page->name)
+            ->click($page->name)
+            ->seePageIs($page->getUrl());
+    }
+
+    public function test_invalid_page_search()
+    {
+        $this->asAdmin()
+            ->visit('/')
+            ->type('<p>test</p>', 'term')
+            ->press('header-search-box-button')
+            ->see('Search Results')
+            ->seeStatusCode(200);
+    }
+
+    public function test_empty_search_redirects_back()
+    {
+        $this->asAdmin()
+            ->visit('/')
+            ->visit('/search/all')
+            ->seePageIs('/');
+    }
+
+    public function test_book_search()
+    {
+        $book = \BookStack\Book::all()->first();
+        $page = $book->pages->last();
+        $chapter = $book->chapters->last();
+
+        $this->asAdmin()
+            ->visit('/search/book/' . $book->id . '?term=' . urlencode($page->name))
+            ->see($page->name)
+
+            ->visit('/search/book/' . $book->id  . '?term=' . urlencode($chapter->name))
+            ->see($chapter->name);
+    }
+
+    public function test_empty_book_search_redirects_back()
+    {
+        $book = \BookStack\Book::all()->first();
+        $this->asAdmin()
+            ->visit('/books')
+            ->visit('/search/book/' . $book->id . '?term=')
+            ->seePageIs('/books');
+    }
+
+
+    public function test_entities_viewable_after_creator_deletion()
+    {
+        // Create required assets and revisions
+        $creator = $this->getNewUser();
+        $updater = $this->getNewUser();
+        $entities = $this->createEntityChainBelongingToUser($creator, $updater);
+        $this->actingAs($creator);
+        app('BookStack\Repos\UserRepo')->destroy($creator);
+        app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
+
+        $this->checkEntitiesViewable($entities);
+    }
+
+    public function test_entities_viewable_after_updater_deletion()
+    {
+        // Create required assets and revisions
+        $creator = $this->getNewUser();
+        $updater = $this->getNewUser();
+        $entities = $this->createEntityChainBelongingToUser($creator, $updater);
+        $this->actingAs($updater);
+        app('BookStack\Repos\UserRepo')->destroy($updater);
+        app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
+
+        $this->checkEntitiesViewable($entities);
+    }
+
+    private function checkEntitiesViewable($entities)
+    {
+        // Check pages and books are visible.
+        $this->asAdmin();
+        $this->visit($entities['book']->getUrl())->seeStatusCode(200)
+            ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
+            ->visit($entities['page']->getUrl())->seeStatusCode(200);
+        // Check revision listing shows no errors.
+        $this->visit($entities['page']->getUrl())
+            ->click('Revisions')->seeStatusCode(200);
+    }
+
 
 }