4 namespace Shaarli\Api\Controllers
;
7 use Shaarli\Config\ConfigManager
;
9 use Slim\Http\Environment
;
10 use Slim\Http\Request
;
11 use Slim\Http\Response
;
13 class PutLinkTest
extends \PHPUnit_Framework_TestCase
16 * @var string datastore to test write operations
18 protected static $testDatastore = 'sandbox/datastore.php';
21 * @var ConfigManager instance
26 * @var \ReferenceLinkDB instance.
28 protected $refDB = null;
31 * @var Container instance.
36 * @var Links controller instance.
38 protected $controller;
41 * Number of JSON field per link.
43 const NB_FIELDS_LINK
= 9;
46 * Before every test, instantiate a new Api with its config, plugins and links.
48 public function setUp()
50 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
51 $this->refDB
= new \
ReferenceLinkDB();
52 $this->refDB
->write(self
::$testDatastore);
54 $this->container
= new Container();
55 $this->container
['conf'] = $this->conf
;
56 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
58 $this->controller
= new Links($this->container
);
60 // Used by index_url().
61 $this->controller
->getCi()['environment'] = [
62 'SERVER_NAME' => 'domain.tld',
69 * After every test, remove the test datastore.
71 public function tearDown()
73 @unlink(self
::$testDatastore);
77 * Test link update without value: reset the link to default values
79 public function testPutLinkMinimal()
81 $env = Environment
::mock([
82 'REQUEST_METHOD' => 'PUT',
85 $request = Request
::createFromEnvironment($env);
87 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
88 $this->assertEquals(200, $response->getStatusCode());
89 $data = json_decode((string) $response->getBody(), true);
90 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
91 $this->assertEquals($id, $data['id']);
92 $this->assertEquals('WDWyig', $data['shorturl']);
93 $this->assertEquals('http://domain.tld/?WDWyig', $data['url']);
94 $this->assertEquals('?WDWyig', $data['title']);
95 $this->assertEquals('', $data['description']);
96 $this->assertEquals([], $data['tags']);
97 $this->assertEquals(false, $data['private']);
99 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
100 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
102 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
106 * Test link update with new values
108 public function testPutLinkWithValues()
110 $env = Environment
::mock([
111 'REQUEST_METHOD' => 'PUT',
112 'CONTENT_TYPE' => 'application/json'
116 'url' => 'http://somewhere.else',
118 'description' => 'Percé jusques au fond du cœur [...]',
119 'tags' => ['corneille', 'rodrigue'],
122 $request = Request
::createFromEnvironment($env);
123 $request = $request->withParsedBody($update);
125 $response = $this->controller
->putLink($request, new Response(), ['id' => $id]);
126 $this->assertEquals(200, $response->getStatusCode());
127 $data = json_decode((string) $response->getBody(), true);
128 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
129 $this->assertEquals($id, $data['id']);
130 $this->assertEquals('WDWyig', $data['shorturl']);
131 $this->assertEquals('http://somewhere.else', $data['url']);
132 $this->assertEquals('Le Cid', $data['title']);
133 $this->assertEquals('Percé jusques au fond du cœur [...]', $data['description']);
134 $this->assertEquals(['corneille', 'rodrigue'], $data['tags']);
135 $this->assertEquals(true, $data['private']);
137 \DateTime
::createFromFormat('Ymd_His', '20150310_114651'),
138 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
140 $this->assertTrue(new \
DateTime('5 seconds ago') < \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated']));
144 * Test link update with an existing URL: 409 Conflict with the existing link as body
146 public function testPutLinkDuplicate()
149 'url' => 'mediagoblin.org/',
150 'title' => 'new entry',
151 'description' => 'shaare description',
152 'tags' => ['one', 'two'],
155 $env = Environment
::mock([
156 'REQUEST_METHOD' => 'PUT',
157 'CONTENT_TYPE' => 'application/json'
160 $request = Request
::createFromEnvironment($env);
161 $request = $request->withParsedBody($link);
162 $response = $this->controller
->putLink($request, new Response(), ['id' => 41]);
164 $this->assertEquals(409, $response->getStatusCode());
165 $data = json_decode((string) $response->getBody(), true);
166 $this->assertEquals(self
::NB_FIELDS_LINK
, count($data));
167 $this->assertEquals(7, $data['id']);
168 $this->assertEquals('IuWvgA', $data['shorturl']);
169 $this->assertEquals('http://mediagoblin.org/', $data['url']);
170 $this->assertEquals('MediaGoblin', $data['title']);
171 $this->assertEquals('A free software media publishing platform #hashtagOther', $data['description']);
172 $this->assertEquals(['gnu', 'media', 'web', '.hidden', 'hashtag'], $data['tags']);
173 $this->assertEquals(false, $data['private']);
175 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130614_184135'),
176 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['created'])
179 \DateTime
::createFromFormat(\LinkDB
::LINK_DATE_FORMAT
, '20130615_184230'),
180 \DateTime
::createFromFormat(\DateTime
::ATOM
, $data['updated'])
185 * Test link update on non existent link => ApiLinkNotFoundException.
187 * @expectedException Shaarli\Api\Exceptions\ApiLinkNotFoundException
188 * @expectedExceptionMessage Link not found
190 public function testGetLink404()
192 $env = Environment
::mock([
193 'REQUEST_METHOD' => 'PUT',
195 $request = Request
::createFromEnvironment($env);
197 $this->controller
->putLink($request, new Response(), ['id' => -1]);