diff options
author | ArthurHoaro <arthur@hoa.ro> | 2019-07-27 12:34:30 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2019-07-27 12:34:30 +0200 |
commit | 38672ba0d1c722e5d6d33a58255ceb55e9410e46 (patch) | |
tree | dae4c7c47532380eac3ae641db99122fc77c93dc /tests/api/controllers/links/PutLinkTest.php | |
parent | 83faedadff76c5bdca036f39f13943f63b27e164 (diff) | |
parent | 1e77e0448bbd25675d8c0fe4a73206ad9048904b (diff) | |
download | Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.gz Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.tar.zst Shaarli-38672ba0d1c722e5d6d33a58255ceb55e9410e46.zip |
Merge tag 'v0.10.4' into stable
Release v0.10.4
Diffstat (limited to 'tests/api/controllers/links/PutLinkTest.php')
-rw-r--r-- | tests/api/controllers/links/PutLinkTest.php | 225 |
1 files changed, 225 insertions, 0 deletions
diff --git a/tests/api/controllers/links/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php new file mode 100644 index 00000000..f276b4c1 --- /dev/null +++ b/tests/api/controllers/links/PutLinkTest.php | |||
@@ -0,0 +1,225 @@ | |||
1 | <?php | ||
2 | |||
3 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | use Shaarli\Config\ConfigManager; | ||
7 | use Slim\Container; | ||
8 | use Slim\Http\Environment; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | class PutLinkTest extends \PHPUnit_Framework_TestCase | ||
13 | { | ||
14 | /** | ||
15 | * @var string datastore to test write operations | ||
16 | */ | ||
17 | protected static $testDatastore = 'sandbox/datastore.php'; | ||
18 | |||
19 | /** | ||
20 | * @var string datastore to test write operations | ||
21 | */ | ||
22 | protected static $testHistory = 'sandbox/history.php'; | ||
23 | |||
24 | /** | ||
25 | * @var ConfigManager instance | ||
26 | */ | ||
27 | protected $conf; | ||
28 | |||
29 | /** | ||
30 | * @var \ReferenceLinkDB instance. | ||
31 | */ | ||
32 | protected $refDB = null; | ||
33 | |||
34 | /** | ||
35 | * @var \History instance. | ||
36 | */ | ||
37 | protected $history; | ||
38 | |||
39 | /** | ||
40 | * @var Container instance. | ||
41 | */ | ||
42 | protected $container; | ||
43 | |||
44 | /** | ||
45 | * @var Links controller instance. | ||
46 | */ | ||
47 | protected $controller; | ||
48 | |||
49 | /** | ||
50 | * Number of JSON field per link. | ||
51 | */ | ||
52 | const NB_FIELDS_LINK = 9; | ||
53 | |||
54 | /** | ||
55 | * Before every test, instantiate a new Api with its config, plugins and links. | ||
56 | */ | ||
57 | public function setUp() | ||
58 | { | ||
59 | $this->conf = new ConfigManager('tests/utils/config/configJson.json.php'); | ||
60 | $this->refDB = new \ReferenceLinkDB(); | ||
61 | $this->refDB->write(self::$testDatastore); | ||
62 | |||
63 | $refHistory = new \ReferenceHistory(); | ||
64 | $refHistory->write(self::$testHistory); | ||
65 | $this->history = new \History(self::$testHistory); | ||
66 | |||
67 | $this->container = new Container(); | ||
68 | $this->container['conf'] = $this->conf; | ||
69 | $this->container['db'] = new \LinkDB(self::$testDatastore, true, false); | ||
70 | $this->container['history'] = new \History(self::$testHistory); | ||
71 | |||
72 | $this->controller = new Links($this->container); | ||
73 | |||
74 | // Used by index_url(). | ||
75 | $this->controller->getCi()['environment'] = [ | ||
76 | 'SERVER_NAME' => 'domain.tld', | ||
77 | 'SERVER_PORT' => 80, | ||
78 | 'SCRIPT_NAME' => '/', | ||
79 | ]; | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * After every test, remove the test datastore. | ||
84 | */ | ||
85 | public function tearDown() | ||
86 | { | ||
87 | @unlink(self::$testDatastore); | ||
88 | @unlink(self::$testHistory); | ||
89 | } | ||
90 | |||
91 | /** | ||
92 | * Test link update without value: reset the link to default values | ||
93 | */ | ||
94 | public function testPutLinkMinimal() | ||
95 | { | ||
96 | $env = Environment::mock([ | ||
97 | 'REQUEST_METHOD' => 'PUT', | ||
98 | ]); | ||
99 | $id = '41'; | ||
100 | $request = Request::createFromEnvironment($env); | ||
101 | |||
102 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | ||
103 | $this->assertEquals(200, $response->getStatusCode()); | ||
104 | $data = json_decode((string) $response->getBody(), true); | ||
105 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
106 | $this->assertEquals($id, $data['id']); | ||
107 | $this->assertEquals('WDWyig', $data['shorturl']); | ||
108 | $this->assertEquals('http://domain.tld/?WDWyig', $data['url']); | ||
109 | $this->assertEquals('?WDWyig', $data['title']); | ||
110 | $this->assertEquals('', $data['description']); | ||
111 | $this->assertEquals([], $data['tags']); | ||
112 | $this->assertEquals(false, $data['private']); | ||
113 | $this->assertEquals( | ||
114 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | ||
115 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
116 | ); | ||
117 | $this->assertTrue( | ||
118 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
119 | ); | ||
120 | |||
121 | $historyEntry = $this->history->getHistory()[0]; | ||
122 | $this->assertEquals(\History::UPDATED, $historyEntry['event']); | ||
123 | $this->assertTrue( | ||
124 | (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime'] | ||
125 | ); | ||
126 | $this->assertEquals($id, $historyEntry['id']); | ||
127 | } | ||
128 | |||
129 | /** | ||
130 | * Test link update with new values | ||
131 | */ | ||
132 | public function testPutLinkWithValues() | ||
133 | { | ||
134 | $env = Environment::mock([ | ||
135 | 'REQUEST_METHOD' => 'PUT', | ||
136 | 'CONTENT_TYPE' => 'application/json' | ||
137 | ]); | ||
138 | $id = 41; | ||
139 | $update = [ | ||
140 | 'url' => 'http://somewhere.else', | ||
141 | 'title' => 'Le Cid', | ||
142 | 'description' => 'Percé jusques au fond du cœur [...]', | ||
143 | 'tags' => ['corneille', 'rodrigue'], | ||
144 | 'private' => true, | ||
145 | ]; | ||
146 | $request = Request::createFromEnvironment($env); | ||
147 | $request = $request->withParsedBody($update); | ||
148 | |||
149 | $response = $this->controller->putLink($request, new Response(), ['id' => $id]); | ||
150 | $this->assertEquals(200, $response->getStatusCode()); | ||
151 | $data = json_decode((string) $response->getBody(), true); | ||
152 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
153 | $this->assertEquals($id, $data['id']); | ||
154 | $this->assertEquals('WDWyig', $data['shorturl']); | ||
155 | $this->assertEquals('http://somewhere.else', $data['url']); | ||
156 | $this->assertEquals('Le Cid', $data['title']); | ||
157 | $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']); | ||
158 | $this->assertEquals(['corneille', 'rodrigue'], $data['tags']); | ||
159 | $this->assertEquals(true, $data['private']); | ||
160 | $this->assertEquals( | ||
161 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | ||
162 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
163 | ); | ||
164 | $this->assertTrue( | ||
165 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
166 | ); | ||
167 | } | ||
168 | |||
169 | /** | ||
170 | * Test link update with an existing URL: 409 Conflict with the existing link as body | ||
171 | */ | ||
172 | public function testPutLinkDuplicate() | ||
173 | { | ||
174 | $link = [ | ||
175 | 'url' => 'mediagoblin.org/', | ||
176 | 'title' => 'new entry', | ||
177 | 'description' => 'shaare description', | ||
178 | 'tags' => ['one', 'two'], | ||
179 | 'private' => true, | ||
180 | ]; | ||
181 | $env = Environment::mock([ | ||
182 | 'REQUEST_METHOD' => 'PUT', | ||
183 | 'CONTENT_TYPE' => 'application/json' | ||
184 | ]); | ||
185 | |||
186 | $request = Request::createFromEnvironment($env); | ||
187 | $request = $request->withParsedBody($link); | ||
188 | $response = $this->controller->putLink($request, new Response(), ['id' => 41]); | ||
189 | |||
190 | $this->assertEquals(409, $response->getStatusCode()); | ||
191 | $data = json_decode((string) $response->getBody(), true); | ||
192 | $this->assertEquals(self::NB_FIELDS_LINK, count($data)); | ||
193 | $this->assertEquals(7, $data['id']); | ||
194 | $this->assertEquals('IuWvgA', $data['shorturl']); | ||
195 | $this->assertEquals('http://mediagoblin.org/', $data['url']); | ||
196 | $this->assertEquals('MediaGoblin', $data['title']); | ||
197 | $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']); | ||
198 | $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']); | ||
199 | $this->assertEquals(false, $data['private']); | ||
200 | $this->assertEquals( | ||
201 | \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'), | ||
202 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
203 | ); | ||
204 | $this->assertEquals( | ||
205 | \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'), | ||
206 | \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
207 | ); | ||
208 | } | ||
209 | |||
210 | /** | ||
211 | * Test link update on non existent link => ApiLinkNotFoundException. | ||
212 | * | ||
213 | * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException | ||
214 | * @expectedExceptionMessage Link not found | ||
215 | */ | ||
216 | public function testGetLink404() | ||
217 | { | ||
218 | $env = Environment::mock([ | ||
219 | 'REQUEST_METHOD' => 'PUT', | ||
220 | ]); | ||
221 | $request = Request::createFromEnvironment($env); | ||
222 | |||
223 | $this->controller->putLink($request, new Response(), ['id' => -1]); | ||
224 | } | ||
225 | } | ||