Skip to content

Commit d3709de

Browse files
committed
Added more tests to increase test coverage
1 parent f60a0c3 commit d3709de

File tree

10 files changed

+97
-33
lines changed

10 files changed

+97
-33
lines changed

app/Entity.php

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ public function matchesOrContains(Entity $entity)
3131

3232
if ($matches) return true;
3333

34-
if ($entity->isA('chapter') && $this->isA('book')) {
35-
return $entity->book_id === $this->id;
36-
}
37-
38-
if ($entity->isA('page') && $this->isA('book')) {
34+
if (($entity->isA('chapter') || $entity->isA('page')) && $this->isA('book')) {
3935
return $entity->book_id === $this->id;
4036
}
4137

@@ -64,15 +60,6 @@ public function views()
6460
return $this->morphMany('BookStack\View', 'viewable');
6561
}
6662

67-
/**
68-
* Get just the views for the current user.
69-
* @return mixed
70-
*/
71-
public function userViews()
72-
{
73-
return $this->views()->where('user_id', '=', auth()->user()->id);
74-
}
75-
7663
/**
7764
* Allows checking of the exact class, Used to check entity type.
7865
* Cleaner method for is_a.

app/Http/Controllers/SearchController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function searchBook(Request $request, $bookId)
6262
return redirect()->back();
6363
}
6464
$searchTerm = $request->get('term');
65-
$whereTerm = [['book_id', '=', $bookId]];
66-
$pages = $this->pageRepo->getBySearch($searchTerm, $whereTerm);
67-
$chapters = $this->chapterRepo->getBySearch($searchTerm, $whereTerm);
65+
$searchWhereTerms = [['book_id', '=', $bookId]];
66+
$pages = $this->pageRepo->getBySearch($searchTerm, $searchWhereTerms);
67+
$chapters = $this->chapterRepo->getBySearch($searchTerm, $searchWhereTerms);
6868
return view('search/book', ['pages' => $pages, 'chapters' => $chapters, 'searchTerm' => $searchTerm]);
6969
}
7070

app/Http/Controllers/UserController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,11 @@ public function update(Request $request, $id)
116116
$this->validate($request, [
117117
'name' => 'required',
118118
'email' => 'required|email|unique:users,email,' . $id,
119-
'password' => 'min:5',
120-
'password-confirm' => 'same:password',
119+
'password' => 'min:5|required_with:password_confirm',
120+
'password-confirm' => 'same:password|required_with:password',
121121
'role' => 'exists:roles,id'
122+
], [
123+
'password-confirm.required_with' => 'Password confirmation required'
122124
]);
123125

124126
$user = $this->user->findOrFail($id);
@@ -132,6 +134,7 @@ public function update(Request $request, $id)
132134
$password = $request->get('password');
133135
$user->password = bcrypt($password);
134136
}
137+
135138
$user->save();
136139
return redirect('/users');
137140
}

app/Role.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ public function attachPermission(Permission $permission)
4343
*/
4444
public static function getDefault()
4545
{
46-
return static::where('name', '=', static::$default)->first();
46+
return static::getRole(static::$default);
47+
}
48+
49+
/**
50+
* Get the role object for the specified role.
51+
* @param $roleName
52+
* @return mixed
53+
*/
54+
public static function getRole($roleName)
55+
{
56+
return static::where('name', '=', $roleName)->first();
4757
}
4858
}

database/seeds/DummyContentSeeder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class DummyContentSeeder extends Seeder
1212
public function run()
1313
{
1414
$user = factory(BookStack\User::class, 1)->create();
15-
$role = \BookStack\Role::where('name', '=', 'admin')->first();
15+
$role = \BookStack\Role::getDefault();
1616
$user->attachRole($role);
1717

1818

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@
2626
<env name="QUEUE_DRIVER" value="sync"/>
2727
<env name="DB_CONNECTION" value="mysql_testing"/>
2828
<env name="MAIL_PRETEND" value="true"/>
29-
<env name="DISABLE_EXTERNAL_SERVICES" value="true"/>
29+
<env name="DISABLE_EXTERNAL_SERVICES" value="false"/>
3030
</php>
3131
</phpunit>

resources/assets/js/controllers.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ module.exports = function (ngApp) {
127127
}]);
128128

129129

130-
ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', function ($scope, $http, $attrs) {
130+
ngApp.controller('BookShowController', ['$scope', '$http', '$attrs', '$sce', function ($scope, $http, $attrs, $sce) {
131131
$scope.searching = false;
132132
$scope.searchTerm = '';
133133
$scope.searchResults = '';
@@ -141,7 +141,7 @@ module.exports = function (ngApp) {
141141
var searchUrl = '/search/book/' + $attrs.bookId;
142142
searchUrl += '?term=' + encodeURIComponent(term);
143143
$http.get(searchUrl).then((response) => {
144-
$scope.searchResults = response.data;
144+
$scope.searchResults = $sce.trustAsHtml(response.data);
145145
});
146146
};
147147

resources/views/users/edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<div class="col-md-6"></div>
1010
<div class="col-md-6 faded">
1111
<div class="action-buttons">
12-
<a href="/users/{{$user->id}}/delete" class="text-neg text-button"><i class="zmdi zmdi-delete"></i>Delete user</a>
12+
<a href="/users/{{$user->id}}/delete" class="text-neg text-button"><i class="zmdi zmdi-delete"></i>Delete User</a>
1313
</div>
1414
</div>
1515
</div>

tests/AuthTest.php

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,10 @@ public function testConfirmedRegistration()
102102
->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
103103
}
104104

105-
public function testUserControl()
105+
public function testUserCreation()
106106
{
107107
$user = factory(\BookStack\User::class)->make();
108-
// Test creation
108+
109109
$this->asAdmin()
110110
->visit('/users')
111111
->click('Add new user')
@@ -118,9 +118,12 @@ public function testUserControl()
118118
->seeInDatabase('users', $user->toArray())
119119
->seePageIs('/users')
120120
->see($user->name);
121-
$user = $user->where('email', '=', $user->email)->first();
121+
}
122122

123-
// Test editing
123+
public function testUserUpdating()
124+
{
125+
$user = \BookStack\User::all()->last();
126+
$password = $user->password;
124127
$this->asAdmin()
125128
->visit('/users')
126129
->click($user->name)
@@ -129,20 +132,58 @@ public function testUserControl()
129132
->type('Barry Scott', '#name')
130133
->press('Save')
131134
->seePageIs('/users')
132-
->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott'])
135+
->seeInDatabase('users', ['id' => $user->id, 'name' => 'Barry Scott', 'password' => $password])
133136
->notSeeInDatabase('users', ['name' => $user->name]);
134-
$user = $user->find($user->id);
137+
}
138+
139+
public function testUserPasswordUpdate()
140+
{
141+
$user = \BookStack\User::all()->last();
142+
$userProfilePage = '/users/' . $user->id;
143+
$this->asAdmin()
144+
->visit($userProfilePage)
145+
->type('newpassword', '#password')
146+
->press('Save')
147+
->seePageIs($userProfilePage)
148+
->see('Password confirmation required')
149+
150+
->type('newpassword', '#password')
151+
->type('newpassword', '#password-confirm')
152+
->press('Save')
153+
->seePageIs('/users');
154+
155+
$userPassword = \BookStack\User::find($user->id)->password;
156+
$this->assertTrue(Hash::check('newpassword', $userPassword));
157+
}
158+
159+
public function testUserDeletion()
160+
{
161+
$userDetails = factory(\BookStack\User::class)->make();
162+
$user = $this->getNewUser($userDetails->toArray());
135163

136-
// Test Deletion
137164
$this->asAdmin()
138165
->visit('/users/' . $user->id)
139-
->click('Delete user')
166+
->click('Delete User')
140167
->see($user->name)
141168
->press('Confirm')
142169
->seePageIs('/users')
143170
->notSeeInDatabase('users', ['name' => $user->name]);
144171
}
145172

173+
public function testUserCannotBeDeletedIfLastAdmin()
174+
{
175+
$adminRole = \BookStack\Role::getRole('admin');
176+
// Ensure we currently only have 1 admin user
177+
$this->assertEquals(1, $adminRole->users()->count());
178+
$user = $adminRole->users->first();
179+
180+
$this->asAdmin()->visit('/users/' . $user->id)
181+
->click('Delete User')
182+
->press('Confirm')
183+
->seePageIs('/users/' . $user->id)
184+
->see('You cannot delete the only admin');
185+
}
186+
146187
public function testLogout()
147188
{
148189
$this->asAdmin()

tests/EntityTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,29 @@ public function testEmptySearchRedirectsBack()
188188
->seePageIs('/');
189189
}
190190

191+
public function testBookSearch()
192+
{
193+
$book = \BookStack\Book::all()->first();
194+
$page = $book->pages->last();
195+
$chapter = $book->chapters->last();
196+
197+
$this->asAdmin()
198+
->visit('/search/book/' . $book->id . '?term=' . urlencode($page->name))
199+
->see($page->name)
200+
201+
->visit('/search/book/' . $book->id . '?term=' . urlencode($chapter->name))
202+
->see($chapter->name);
203+
}
204+
205+
public function testEmptyBookSearchRedirectsBack()
206+
{
207+
$book = \BookStack\Book::all()->first();
208+
$this->asAdmin()
209+
->visit('/books')
210+
->visit('/search/book/' . $book->id . '?term=')
211+
->seePageIs('/books');
212+
}
213+
191214

192215
public function testEntitiesViewableAfterCreatorDeletion()
193216
{

0 commit comments

Comments
 (0)