]> BookStack Code Mirror - bookstack/blob - tests/EntityTest.php
Change application namespace to BookStack
[bookstack] / tests / EntityTest.php
1 <?php
2
3 class EntityTest extends TestCase
4 {
5
6     public function testEntityCreation()
7     {
8
9         // Test Creation
10         $book = $this->bookCreation();
11         $chapter = $this->chapterCreation($book);
12         $page = $this->pageCreation($chapter);
13
14         // Test Updating
15         $book = $this->bookUpdate($book);
16
17         // Test Deletion
18         $this->bookDelete($book);
19     }
20
21     public function bookDelete(\BookStack\Book $book)
22     {
23         $this->asAdmin()
24             ->visit($book->getUrl())
25             // Check link works correctly
26             ->click('Delete')
27             ->seePageIs($book->getUrl() . '/delete')
28             // Ensure the book name is show to user
29             ->see($book->name)
30             ->press('Confirm')
31             ->seePageIs('/books')
32             ->notSeeInDatabase('books', ['id' => $book->id]);
33     }
34
35     public function bookUpdate(\BookStack\Book $book)
36     {
37         $newName = $book->name . ' Updated';
38         $this->asAdmin()
39             // Go to edit screen
40             ->visit($book->getUrl() . '/edit')
41             ->see($book->name)
42             // Submit new name
43             ->type($newName, '#name')
44             ->press('Save Book')
45             // Check page url and text
46             ->seePageIs($book->getUrl() . '-updated')
47             ->see($newName);
48
49         return \BookStack\Book::find($book->id);
50     }
51
52     public function pageCreation($chapter)
53     {
54         $page = factory(\BookStack\Page::class)->make([
55             'name' => 'My First Page'
56         ]);
57
58         $this->asAdmin()
59             // Navigate to page create form
60             ->visit($chapter->getUrl())
61             ->click('New Page')
62             ->seePageIs($chapter->getUrl() . '/create-page')
63             // Fill out form
64             ->type($page->name, '#name')
65             ->type($page->html, '#html')
66             ->press('Save Page')
67             // Check redirect and page
68             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
69             ->see($page->name);
70
71         $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
72         return $page;
73     }
74
75     public function chapterCreation(\BookStack\Book $book)
76     {
77         $chapter = factory(\BookStack\Chapter::class)->make([
78             'name' => 'My First Chapter'
79         ]);
80
81         $this->asAdmin()
82             // Navigate to chapter create page
83             ->visit($book->getUrl())
84             ->click('New Chapter')
85             ->seePageIs($book->getUrl() . '/chapter/create')
86             // Fill out form
87             ->type($chapter->name, '#name')
88             ->type($chapter->description, '#description')
89             ->press('Save Chapter')
90             // Check redirect and landing page
91             ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
92             ->see($chapter->name)->see($chapter->description);
93
94         $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
95         return $chapter;
96     }
97
98     public function bookCreation()
99     {
100         $book = factory(\BookStack\Book::class)->make([
101             'name' => 'My First Book'
102         ]);
103         $this->asAdmin()
104             ->visit('/books')
105             // Choose to create a book
106             ->click('Add new book')
107             ->seePageIs('/books/create')
108             // Fill out form & save
109             ->type($book->name, '#name')
110             ->type($book->description, '#description')
111             ->press('Save Book')
112             // Check it redirects correctly
113             ->seePageIs('/books/my-first-book')
114             ->see($book->name)->see($book->description);
115
116         $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
117         return $book;
118     }
119
120
121 }