]> BookStack Code Mirror - bookstack/blob - tests/TestCase.php
Merge pull request #1826 from BookStackApp/api_origins
[bookstack] / tests / TestCase.php
1 <?php namespace Tests;
2
3 use BookStack\Entities\Entity;
4 use Illuminate\Foundation\Testing\DatabaseTransactions;
5 use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
6
7 abstract class TestCase extends BaseTestCase
8 {
9     use CreatesApplication;
10     use DatabaseTransactions;
11     use SharedTestHelpers;
12
13     /**
14      * The base URL to use while testing the application.
15      * @var string
16      */
17     protected $baseUrl = 'http://localhost';
18
19     /**
20      * Assert a permission error has occurred.
21      * @param TestResponse $response
22      * @return TestCase
23      */
24     protected function assertPermissionError(TestResponse $response)
25     {
26         $response->assertRedirect('/');
27         $this->assertSessionHas('error');
28         session()->remove('error');
29         return $this;
30     }
31
32     /**
33      * Assert the session contains a specific entry.
34      * @param string $key
35      * @return $this
36      */
37     protected function assertSessionHas(string $key)
38     {
39         $this->assertTrue(session()->has($key), "Session does not contain a [{$key}] entry");
40         return $this;
41     }
42
43     /**
44      * Override of the get method so we can get visibility of custom TestResponse methods.
45      * @param  string  $uri
46      * @param  array  $headers
47      * @return TestResponse
48      */
49     public function get($uri, array $headers = [])
50     {
51         return parent::get($uri, $headers);
52     }
53
54     /**
55      * Create the test response instance from the given response.
56      *
57      * @param  \Illuminate\Http\Response $response
58      * @return TestResponse
59      */
60     protected function createTestResponse($response)
61     {
62         return TestResponse::fromBaseResponse($response);
63     }
64
65     /**
66      * Assert that an activity entry exists of the given key.
67      * Checks the activity belongs to the given entity if provided.
68      */
69     protected function assertActivityExists(string $key, Entity $entity = null)
70     {
71         $detailsToCheck = ['key' => $key];
72
73         if ($entity) {
74             $detailsToCheck['entity_type'] = $entity->getMorphClass();
75             $detailsToCheck['entity_id'] = $entity->id;
76         }
77
78         $this->assertDatabaseHas('activities', $detailsToCheck);
79     }
80 }