]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/api/controllers/links/PutLinkTest.php
lint: apply phpcbf to tests/
[github/shaarli/Shaarli.git] / tests / api / controllers / links / PutLinkTest.php
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(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
118
119 $historyEntry = $this->history->getHistory()[0];
120 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
121 $this->assertTrue(
122 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
123 );
124 $this->assertEquals($id, $historyEntry['id']);
125 }
126
127 /**
128 * Test link update with new values
129 */
130 public function testPutLinkWithValues()
131 {
132 $env = Environment::mock([
133 'REQUEST_METHOD' => 'PUT',
134 'CONTENT_TYPE' => 'application/json'
135 ]);
136 $id = 41;
137 $update = [
138 'url' => 'http://somewhere.else',
139 'title' => 'Le Cid',
140 'description' => 'Percé jusques au fond du cœur [...]',
141 'tags' => ['corneille', 'rodrigue'],
142 'private' => true,
143 ];
144 $request = Request::createFromEnvironment($env);
145 $request = $request->withParsedBody($update);
146
147 $response = $this->controller->putLink($request, new Response(), ['id' => $id]);
148 $this->assertEquals(200, $response->getStatusCode());
149 $data = json_decode((string) $response->getBody(), true);
150 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
151 $this->assertEquals($id, $data['id']);
152 $this->assertEquals('WDWyig', $data['shorturl']);
153 $this->assertEquals('http://somewhere.else', $data['url']);
154 $this->assertEquals('Le Cid', $data['title']);
155 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
156 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
157 $this->assertEquals(true, $data['private']);
158 $this->assertEquals(
159 \DateTime::createFromFormat('Ymd_His', '20150310_114651'),
160 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
161 );
162 $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']));
163 }
164
165 /**
166 * Test link update with an existing URL: 409 Conflict with the existing link as body
167 */
168 public function testPutLinkDuplicate()
169 {
170 $link = [
171 'url' => 'mediagoblin.org/',
172 'title' => 'new entry',
173 'description' => 'shaare description',
174 'tags' => ['one', 'two'],
175 'private' => true,
176 ];
177 $env = Environment::mock([
178 'REQUEST_METHOD' => 'PUT',
179 'CONTENT_TYPE' => 'application/json'
180 ]);
181
182 $request = Request::createFromEnvironment($env);
183 $request = $request->withParsedBody($link);
184 $response = $this->controller->putLink($request, new Response(), ['id' => 41]);
185
186 $this->assertEquals(409, $response->getStatusCode());
187 $data = json_decode((string) $response->getBody(), true);
188 $this->assertEquals(self::NB_FIELDS_LINK, count($data));
189 $this->assertEquals(7, $data['id']);
190 $this->assertEquals('IuWvgA', $data['shorturl']);
191 $this->assertEquals('http://mediagoblin.org/', $data['url']);
192 $this->assertEquals('MediaGoblin', $data['title']);
193 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
194 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
195 $this->assertEquals(false, $data['private']);
196 $this->assertEquals(
197 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
198 \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])
199 );
200 $this->assertEquals(
201 \DateTime::createFromFormat(\LinkDB::LINK_DATE_FORMAT, '20130615_184230'),
202 \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])
203 );
204 }
205
206 /**
207 * Test link update on non existent link => ApiLinkNotFoundException.
208 *
209 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
210 * @expectedExceptionMessage Link not found
211 */
212 public function testGetLink404()
213 {
214 $env = Environment::mock([
215 'REQUEST_METHOD' => 'PUT',
216 ]);
217 $request = Request::createFromEnvironment($env);
218
219 $this->controller->putLink($request, new Response(), ['id' => -1]);
220 }
221 }