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