]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/api/controllers/history/HistoryTest.php
Merge pull request #1182 from ArthurHoaro/feature/session-protection-stay-login
[github/shaarli/Shaarli.git] / tests / api / controllers / history / HistoryTest.php
CommitLineData
61d40693
A
1<?php
2
61d40693
A
3namespace Shaarli\Api\Controllers;
4
61d40693 5use Shaarli\Config\ConfigManager;
dea72c71 6use Shaarli\History;
61d40693
A
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12require_once 'tests/utils/ReferenceHistory.php';
13
dea72c71 14class HistoryTest extends \PHPUnit\Framework\TestCase
61d40693
A
15{
16 /**
17 * @var string datastore to test write operations
18 */
19 protected static $testHistory = 'sandbox/history.php';
20
21 /**
22 * @var ConfigManager instance
23 */
24 protected $conf;
25
26 /**
27 * @var \ReferenceHistory instance.
28 */
29 protected $refHistory = null;
30
61d40693
A
31 /**
32 * @var Container instance.
33 */
34 protected $container;
35
36 /**
dea72c71 37 * @var HistoryController controller instance.
61d40693
A
38 */
39 protected $controller;
40
41 /**
42 * Before every test, instantiate a new Api with its config, plugins and links.
43 */
44 public function setUp()
45 {
46 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
47 $this->refHistory = new \ReferenceHistory();
48 $this->refHistory->write(self::$testHistory);
61d40693
A
49 $this->container = new Container();
50 $this->container['conf'] = $this->conf;
51 $this->container['db'] = true;
dea72c71 52 $this->container['history'] = new History(self::$testHistory);
61d40693 53
dea72c71 54 $this->controller = new HistoryController($this->container);
61d40693
A
55 }
56
57 /**
58 * After every test, remove the test datastore.
59 */
60 public function tearDown()
61 {
62 @unlink(self::$testHistory);
63 }
64
65 /**
66 * Test /history service without parameter.
67 */
68 public function testGetHistory()
69 {
70 $env = Environment::mock([
71 'REQUEST_METHOD' => 'GET',
72 ]);
73 $request = Request::createFromEnvironment($env);
74
75 $response = $this->controller->getHistory($request, new Response());
76 $this->assertEquals(200, $response->getStatusCode());
77 $data = json_decode((string) $response->getBody(), true);
78
79 $this->assertEquals($this->refHistory->count(), count($data));
80
dea72c71 81 $this->assertEquals(History::DELETED, $data[0]['event']);
61d40693
A
82 $this->assertEquals(
83 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
84 $data[0]['datetime']
85 );
86 $this->assertEquals(124, $data[0]['id']);
87
dea72c71 88 $this->assertEquals(History::SETTINGS, $data[1]['event']);
61d40693
A
89 $this->assertEquals(
90 \DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
91 $data[1]['datetime']
92 );
93 $this->assertNull($data[1]['id']);
94
dea72c71 95 $this->assertEquals(History::UPDATED, $data[2]['event']);
61d40693
A
96 $this->assertEquals(
97 \DateTime::createFromFormat('Ymd_His', '20170301_121214')->format(\DateTime::ATOM),
98 $data[2]['datetime']
99 );
100 $this->assertEquals(123, $data[2]['id']);
101
dea72c71 102 $this->assertEquals(History::CREATED, $data[3]['event']);
61d40693
A
103 $this->assertEquals(
104 \DateTime::createFromFormat('Ymd_His', '20170201_121214')->format(\DateTime::ATOM),
105 $data[3]['datetime']
106 );
107 $this->assertEquals(124, $data[3]['id']);
108
dea72c71 109 $this->assertEquals(History::CREATED, $data[4]['event']);
61d40693
A
110 $this->assertEquals(
111 \DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
112 $data[4]['datetime']
113 );
114 $this->assertEquals(123, $data[4]['id']);
115 }
116
117 /**
118 * Test /history service with limit parameter.
119 */
120 public function testGetHistoryLimit()
121 {
122 $env = Environment::mock([
123 'REQUEST_METHOD' => 'GET',
124 'QUERY_STRING' => 'limit=1'
125 ]);
126 $request = Request::createFromEnvironment($env);
127
128 $response = $this->controller->getHistory($request, new Response());
129 $this->assertEquals(200, $response->getStatusCode());
130 $data = json_decode((string) $response->getBody(), true);
131
132 $this->assertEquals(1, count($data));
133
dea72c71 134 $this->assertEquals(History::DELETED, $data[0]['event']);
61d40693
A
135 $this->assertEquals(
136 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
137 $data[0]['datetime']
138 );
139 $this->assertEquals(124, $data[0]['id']);
140 }
141
142 /**
143 * Test /history service with offset parameter.
144 */
145 public function testGetHistoryOffset()
146 {
147 $env = Environment::mock([
148 'REQUEST_METHOD' => 'GET',
149 'QUERY_STRING' => 'offset=4'
150 ]);
151 $request = Request::createFromEnvironment($env);
152
153 $response = $this->controller->getHistory($request, new Response());
154 $this->assertEquals(200, $response->getStatusCode());
155 $data = json_decode((string) $response->getBody(), true);
156
157 $this->assertEquals(1, count($data));
158
dea72c71 159 $this->assertEquals(History::CREATED, $data[0]['event']);
61d40693
A
160 $this->assertEquals(
161 \DateTime::createFromFormat('Ymd_His', '20170101_121212')->format(\DateTime::ATOM),
162 $data[0]['datetime']
163 );
164 $this->assertEquals(123, $data[0]['id']);
165 }
166
167 /**
168 * Test /history service with since parameter.
169 */
170 public function testGetHistorySince()
171 {
172 $env = Environment::mock([
173 'REQUEST_METHOD' => 'GET',
174 'QUERY_STRING' => 'since=2017-03-03T00:00:00%2B00:00'
175 ]);
176 $request = Request::createFromEnvironment($env);
177
178 $response = $this->controller->getHistory($request, new Response());
179 $this->assertEquals(200, $response->getStatusCode());
180 $data = json_decode((string) $response->getBody(), true);
181
182 $this->assertEquals(1, count($data));
183
dea72c71 184 $this->assertEquals(History::DELETED, $data[0]['event']);
61d40693
A
185 $this->assertEquals(
186 \DateTime::createFromFormat('Ymd_His', '20170303_121216')->format(\DateTime::ATOM),
187 $data[0]['datetime']
188 );
189 $this->assertEquals(124, $data[0]['id']);
190 }
191
192 /**
193 * Test /history service with since parameter.
194 */
195 public function testGetHistorySinceOffsetLimit()
196 {
197 $env = Environment::mock([
198 'REQUEST_METHOD' => 'GET',
199 'QUERY_STRING' => 'since=2017-02-01T00:00:00%2B00:00&offset=1&limit=1'
200 ]);
201 $request = Request::createFromEnvironment($env);
202
203 $response = $this->controller->getHistory($request, new Response());
204 $this->assertEquals(200, $response->getStatusCode());
205 $data = json_decode((string) $response->getBody(), true);
206
207 $this->assertEquals(1, count($data));
208
dea72c71 209 $this->assertEquals(History::SETTINGS, $data[0]['event']);
61d40693
A
210 $this->assertEquals(
211 \DateTime::createFromFormat('Ymd_His', '20170302_121215')->format(\DateTime::ATOM),
212 $data[0]['datetime']
213 );
214 }
215}