aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/api/controllers/tags
diff options
context:
space:
mode:
Diffstat (limited to 'tests/api/controllers/tags')
-rw-r--r--tests/api/controllers/tags/DeleteTagTest.php164
-rw-r--r--tests/api/controllers/tags/GetTagNameTest.php129
-rw-r--r--tests/api/controllers/tags/GetTagsTest.php209
-rw-r--r--tests/api/controllers/tags/PutTagTest.php208
4 files changed, 710 insertions, 0 deletions
diff --git a/tests/api/controllers/tags/DeleteTagTest.php b/tests/api/controllers/tags/DeleteTagTest.php
new file mode 100644
index 00000000..e0787ce2
--- /dev/null
+++ b/tests/api/controllers/tags/DeleteTagTest.php
@@ -0,0 +1,164 @@
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
6use Shaarli\Config\ConfigManager;
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12class DeleteTagTest 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 \LinkDB instance.
36 */
37 protected $linkDB;
38
39 /**
40 * @var \History instance.
41 */
42 protected $history;
43
44 /**
45 * @var Container instance.
46 */
47 protected $container;
48
49 /**
50 * @var Tags controller instance.
51 */
52 protected $controller;
53
54 /**
55 * Before each 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');
60 $this->refDB = new \ReferenceLinkDB();
61 $this->refDB->write(self::$testDatastore);
62 $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
63 $refHistory = new \ReferenceHistory();
64 $refHistory->write(self::$testHistory);
65 $this->history = new \History(self::$testHistory);
66 $this->container = new Container();
67 $this->container['conf'] = $this->conf;
68 $this->container['db'] = $this->linkDB;
69 $this->container['history'] = $this->history;
70
71 $this->controller = new Tags($this->container);
72 }
73
74 /**
75 * After each test, remove the test datastore.
76 */
77 public function tearDown()
78 {
79 @unlink(self::$testDatastore);
80 @unlink(self::$testHistory);
81 }
82
83 /**
84 * Test DELETE tag endpoint: the tag should be removed.
85 */
86 public function testDeleteTagValid()
87 {
88 $tagName = 'gnu';
89 $tags = $this->linkDB->linksCountPerTag();
90 $this->assertTrue($tags[$tagName] > 0);
91 $env = Environment::mock([
92 'REQUEST_METHOD' => 'DELETE',
93 ]);
94 $request = Request::createFromEnvironment($env);
95
96 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
97 $this->assertEquals(204, $response->getStatusCode());
98 $this->assertEmpty((string) $response->getBody());
99
100 $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
101 $tags = $this->linkDB->linksCountPerTag();
102 $this->assertFalse(isset($tags[$tagName]));
103
104 // 2 links affected
105 $historyEntry = $this->history->getHistory()[0];
106 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
107 $this->assertTrue(
108 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
109 );
110 $historyEntry = $this->history->getHistory()[1];
111 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
112 $this->assertTrue(
113 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
114 );
115 }
116
117 /**
118 * Test DELETE tag endpoint: the tag should be removed.
119 */
120 public function testDeleteTagCaseSensitivity()
121 {
122 $tagName = 'sTuff';
123 $tags = $this->linkDB->linksCountPerTag();
124 $this->assertTrue($tags[$tagName] > 0);
125 $env = Environment::mock([
126 'REQUEST_METHOD' => 'DELETE',
127 ]);
128 $request = Request::createFromEnvironment($env);
129
130 $response = $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
131 $this->assertEquals(204, $response->getStatusCode());
132 $this->assertEmpty((string) $response->getBody());
133
134 $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
135 $tags = $this->linkDB->linksCountPerTag();
136 $this->assertFalse(isset($tags[$tagName]));
137 $this->assertTrue($tags[strtolower($tagName)] > 0);
138
139 $historyEntry = $this->history->getHistory()[0];
140 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
141 $this->assertTrue(
142 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
143 );
144 }
145
146 /**
147 * Test DELETE tag endpoint: reach not existing tag.
148 *
149 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
150 * @expectedExceptionMessage Tag not found
151 */
152 public function testDeleteLink404()
153 {
154 $tagName = 'nopenope';
155 $tags = $this->linkDB->linksCountPerTag();
156 $this->assertFalse(isset($tags[$tagName]));
157 $env = Environment::mock([
158 'REQUEST_METHOD' => 'DELETE',
159 ]);
160 $request = Request::createFromEnvironment($env);
161
162 $this->controller->deleteTag($request, new Response(), ['tagName' => $tagName]);
163 }
164}
diff --git a/tests/api/controllers/tags/GetTagNameTest.php b/tests/api/controllers/tags/GetTagNameTest.php
new file mode 100644
index 00000000..afac228e
--- /dev/null
+++ b/tests/api/controllers/tags/GetTagNameTest.php
@@ -0,0 +1,129 @@
1<?php
2
3namespace Shaarli\Api\Controllers;
4
5use Shaarli\Config\ConfigManager;
6
7use Slim\Container;
8use Slim\Http\Environment;
9use Slim\Http\Request;
10use Slim\Http\Response;
11
12/**
13 * Class GetTagNameTest
14 *
15 * Test getTag by tag name API service.
16 *
17 * @package Shaarli\Api\Controllers
18 */
19class GetTagNameTest extends \PHPUnit_Framework_TestCase
20{
21 /**
22 * @var string datastore to test write operations
23 */
24 protected static $testDatastore = 'sandbox/datastore.php';
25
26 /**
27 * @var ConfigManager instance
28 */
29 protected $conf;
30
31 /**
32 * @var \ReferenceLinkDB instance.
33 */
34 protected $refDB = null;
35
36 /**
37 * @var Container instance.
38 */
39 protected $container;
40
41 /**
42 * @var Tags controller instance.
43 */
44 protected $controller;
45
46 /**
47 * Number of JSON fields per link.
48 */
49 const NB_FIELDS_TAG = 2;
50
51 /**
52 * Before each test, instantiate a new Api with its config, plugins and links.
53 */
54 public function setUp()
55 {
56 $this->conf = new ConfigManager('tests/utils/config/configJson');
57 $this->refDB = new \ReferenceLinkDB();
58 $this->refDB->write(self::$testDatastore);
59
60 $this->container = new Container();
61 $this->container['conf'] = $this->conf;
62 $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
63 $this->container['history'] = null;
64
65 $this->controller = new Tags($this->container);
66 }
67
68 /**
69 * After each test, remove the test datastore.
70 */
71 public function tearDown()
72 {
73 @unlink(self::$testDatastore);
74 }
75
76 /**
77 * Test basic getTag service: return gnu tag with 2 occurrences.
78 */
79 public function testGetTag()
80 {
81 $tagName = 'gnu';
82 $env = Environment::mock([
83 'REQUEST_METHOD' => 'GET',
84 ]);
85 $request = Request::createFromEnvironment($env);
86
87 $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
88 $this->assertEquals(200, $response->getStatusCode());
89 $data = json_decode((string) $response->getBody(), true);
90 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
91 $this->assertEquals($tagName, $data['name']);
92 $this->assertEquals(2, $data['occurrences']);
93 }
94
95 /**
96 * Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
97 */
98 public function testGetTagNotCaseSensitive()
99 {
100 $tagName = 'sTuff';
101 $env = Environment::mock([
102 'REQUEST_METHOD' => 'GET',
103 ]);
104 $request = Request::createFromEnvironment($env);
105
106 $response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
107 $this->assertEquals(200, $response->getStatusCode());
108 $data = json_decode((string) $response->getBody(), true);
109 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
110 $this->assertEquals($tagName, $data['name']);
111 $this->assertEquals(2, $data['occurrences']);
112 }
113
114 /**
115 * Test basic getTag service: get non existent tag => ApiTagNotFoundException.
116 *
117 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
118 * @expectedExceptionMessage Tag not found
119 */
120 public function testGetTag404()
121 {
122 $env = Environment::mock([
123 'REQUEST_METHOD' => 'GET',
124 ]);
125 $request = Request::createFromEnvironment($env);
126
127 $this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
128 }
129}
diff --git a/tests/api/controllers/tags/GetTagsTest.php b/tests/api/controllers/tags/GetTagsTest.php
new file mode 100644
index 00000000..3fab31b0
--- /dev/null
+++ b/tests/api/controllers/tags/GetTagsTest.php
@@ -0,0 +1,209 @@
1<?php
2namespace Shaarli\Api\Controllers;
3
4use Shaarli\Config\ConfigManager;
5
6use Slim\Container;
7use Slim\Http\Environment;
8use Slim\Http\Request;
9use Slim\Http\Response;
10
11/**
12 * Class GetTagsTest
13 *
14 * Test get tag list REST API service.
15 *
16 * @package Shaarli\Api\Controllers
17 */
18class GetTagsTest extends \PHPUnit_Framework_TestCase
19{
20 /**
21 * @var string datastore to test write operations
22 */
23 protected static $testDatastore = 'sandbox/datastore.php';
24
25 /**
26 * @var ConfigManager instance
27 */
28 protected $conf;
29
30 /**
31 * @var \ReferenceLinkDB instance.
32 */
33 protected $refDB = null;
34
35 /**
36 * @var Container instance.
37 */
38 protected $container;
39
40 /**
41 * @var \LinkDB instance.
42 */
43 protected $linkDB;
44
45 /**
46 * @var Tags controller instance.
47 */
48 protected $controller;
49
50 /**
51 * Number of JSON field per link.
52 */
53 const NB_FIELDS_TAG = 2;
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');
61 $this->refDB = new \ReferenceLinkDB();
62 $this->refDB->write(self::$testDatastore);
63
64 $this->container = new Container();
65 $this->container['conf'] = $this->conf;
66 $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
67 $this->container['db'] = $this->linkDB;
68 $this->container['history'] = null;
69
70 $this->controller = new Tags($this->container);
71 }
72
73 /**
74 * After every test, remove the test datastore.
75 */
76 public function tearDown()
77 {
78 @unlink(self::$testDatastore);
79 }
80
81 /**
82 * Test basic getTags service: returns all tags.
83 */
84 public function testGetTagsAll()
85 {
86 $tags = $this->linkDB->linksCountPerTag();
87 $env = Environment::mock([
88 'REQUEST_METHOD' => 'GET',
89 ]);
90 $request = Request::createFromEnvironment($env);
91
92 $response = $this->controller->getTags($request, new Response());
93 $this->assertEquals(200, $response->getStatusCode());
94 $data = json_decode((string) $response->getBody(), true);
95 $this->assertEquals(count($tags), count($data));
96
97 // Check order
98 $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
99 $this->assertEquals('web', $data[0]['name']);
100 $this->assertEquals(4, $data[0]['occurrences']);
101 $this->assertEquals(self::NB_FIELDS_TAG, count($data[1]));
102 $this->assertEquals('cartoon', $data[1]['name']);
103 $this->assertEquals(3, $data[1]['occurrences']);
104 // Case insensitive
105 $this->assertEquals(self::NB_FIELDS_TAG, count($data[5]));
106 $this->assertEquals('sTuff', $data[5]['name']);
107 $this->assertEquals(2, $data[5]['occurrences']);
108 // End
109 $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1]));
110 $this->assertEquals('w3c', $data[count($data) - 1]['name']);
111 $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
112 }
113
114 /**
115 * Test getTags service with offset and limit parameter:
116 * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
117 */
118 public function testGetTagsOffsetLimit()
119 {
120 $env = Environment::mock([
121 'REQUEST_METHOD' => 'GET',
122 'QUERY_STRING' => 'offset=1&limit=1'
123 ]);
124 $request = Request::createFromEnvironment($env);
125 $response = $this->controller->getTags($request, new Response());
126 $this->assertEquals(200, $response->getStatusCode());
127 $data = json_decode((string) $response->getBody(), true);
128 $this->assertEquals(1, count($data));
129 $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
130 $this->assertEquals('cartoon', $data[0]['name']);
131 $this->assertEquals(3, $data[0]['occurrences']);
132 }
133
134 /**
135 * Test getTags with limit=all (return all tags).
136 */
137 public function testGetTagsLimitAll()
138 {
139 $tags = $this->linkDB->linksCountPerTag();
140 $env = Environment::mock([
141 'REQUEST_METHOD' => 'GET',
142 'QUERY_STRING' => 'limit=all'
143 ]);
144 $request = Request::createFromEnvironment($env);
145 $response = $this->controller->getTags($request, new Response());
146 $this->assertEquals(200, $response->getStatusCode());
147 $data = json_decode((string) $response->getBody(), true);
148 $this->assertEquals(count($tags), count($data));
149 }
150
151 /**
152 * Test getTags service with offset and limit parameter:
153 * limit=1 and offset=1 should not return any tag
154 */
155 public function testGetTagsOffsetTooHigh()
156 {
157 $env = Environment::mock([
158 'REQUEST_METHOD' => 'GET',
159 'QUERY_STRING' => 'offset=100'
160 ]);
161 $request = Request::createFromEnvironment($env);
162 $response = $this->controller->getTags($request, new Response());
163 $this->assertEquals(200, $response->getStatusCode());
164 $data = json_decode((string) $response->getBody(), true);
165 $this->assertEmpty(count($data));
166 }
167
168 /**
169 * Test getTags with visibility parameter set to private
170 */
171 public function testGetTagsVisibilityPrivate()
172 {
173 $tags = $this->linkDB->linksCountPerTag([], 'private');
174 $env = Environment::mock([
175 'REQUEST_METHOD' => 'GET',
176 'QUERY_STRING' => 'visibility=private'
177 ]);
178 $request = Request::createFromEnvironment($env);
179 $response = $this->controller->getTags($request, new Response());
180 $this->assertEquals(200, $response->getStatusCode());
181 $data = json_decode((string) $response->getBody(), true);
182 $this->assertEquals(count($tags), count($data));
183 $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
184 $this->assertEquals('Mercurial', $data[0]['name']);
185 $this->assertEquals(1, $data[0]['occurrences']);
186 }
187
188 /**
189 * Test getTags with visibility parameter set to public
190 */
191 public function testGetTagsVisibilityPublic()
192 {
193 $tags = $this->linkDB->linksCountPerTag([], 'public');
194 $env = Environment::mock(
195 [
196 'REQUEST_METHOD' => 'GET',
197 'QUERY_STRING' => 'visibility=public'
198 ]
199 );
200 $request = Request::createFromEnvironment($env);
201 $response = $this->controller->getTags($request, new Response());
202 $this->assertEquals(200, $response->getStatusCode());
203 $data = json_decode((string)$response->getBody(), true);
204 $this->assertEquals(count($tags), count($data));
205 $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
206 $this->assertEquals('web', $data[0]['name']);
207 $this->assertEquals(3, $data[0]['occurrences']);
208 }
209}
diff --git a/tests/api/controllers/tags/PutTagTest.php b/tests/api/controllers/tags/PutTagTest.php
new file mode 100644
index 00000000..38017243
--- /dev/null
+++ b/tests/api/controllers/tags/PutTagTest.php
@@ -0,0 +1,208 @@
1<?php
2
3
4namespace Shaarli\Api\Controllers;
5
6use Shaarli\Api\Exceptions\ApiBadParametersException;
7use Shaarli\Config\ConfigManager;
8use Slim\Container;
9use Slim\Http\Environment;
10use Slim\Http\Request;
11use Slim\Http\Response;
12
13class PutTagTest extends \PHPUnit_Framework_TestCase
14{
15 /**
16 * @var string datastore to test write operations
17 */
18 protected static $testDatastore = 'sandbox/datastore.php';
19
20 /**
21 * @var string datastore to test write operations
22 */
23 protected static $testHistory = 'sandbox/history.php';
24
25 /**
26 * @var ConfigManager instance
27 */
28 protected $conf;
29
30 /**
31 * @var \ReferenceLinkDB instance.
32 */
33 protected $refDB = null;
34
35 /**
36 * @var \History instance.
37 */
38 protected $history;
39
40 /**
41 * @var Container instance.
42 */
43 protected $container;
44
45 /**
46 * @var \LinkDB instance.
47 */
48 protected $linkDB;
49
50 /**
51 * @var Tags controller instance.
52 */
53 protected $controller;
54
55 /**
56 * Number of JSON field per link.
57 */
58 const NB_FIELDS_TAG = 2;
59
60 /**
61 * Before every test, instantiate a new Api with its config, plugins and links.
62 */
63 public function setUp()
64 {
65 $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
66 $this->refDB = new \ReferenceLinkDB();
67 $this->refDB->write(self::$testDatastore);
68
69 $refHistory = new \ReferenceHistory();
70 $refHistory->write(self::$testHistory);
71 $this->history = new \History(self::$testHistory);
72
73 $this->container = new Container();
74 $this->container['conf'] = $this->conf;
75 $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
76 $this->container['db'] = $this->linkDB;
77 $this->container['history'] = $this->history;
78
79 $this->controller = new Tags($this->container);
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 tags update
93 */
94 public function testPutLinkValid()
95 {
96 $env = Environment::mock([
97 'REQUEST_METHOD' => 'PUT',
98 ]);
99 $tagName = 'gnu';
100 $update = ['name' => $newName = 'newtag'];
101 $request = Request::createFromEnvironment($env);
102 $request = $request->withParsedBody($update);
103
104 $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
105 $this->assertEquals(200, $response->getStatusCode());
106 $data = json_decode((string) $response->getBody(), true);
107 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
108 $this->assertEquals($newName, $data['name']);
109 $this->assertEquals(2, $data['occurrences']);
110
111 $tags = $this->linkDB->linksCountPerTag();
112 $this->assertNotTrue(isset($tags[$tagName]));
113 $this->assertEquals(2, $tags[$newName]);
114
115 $historyEntry = $this->history->getHistory()[0];
116 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
117 $this->assertTrue(
118 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
119 );
120 $historyEntry = $this->history->getHistory()[1];
121 $this->assertEquals(\History::UPDATED, $historyEntry['event']);
122 $this->assertTrue(
123 (new \DateTime())->add(\DateInterval::createFromDateString('-5 seconds')) < $historyEntry['datetime']
124 );
125 }
126
127 /**
128 * Test tag update with an existing tag: they should be merged
129 */
130 public function testPutTagMerge()
131 {
132 $tagName = 'gnu';
133 $newName = 'w3c';
134
135 $tags = $this->linkDB->linksCountPerTag();
136 $this->assertEquals(1, $tags[$newName]);
137 $this->assertEquals(2, $tags[$tagName]);
138
139 $env = Environment::mock([
140 'REQUEST_METHOD' => 'PUT',
141 ]);
142 $update = ['name' => $newName];
143 $request = Request::createFromEnvironment($env);
144 $request = $request->withParsedBody($update);
145
146 $response = $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
147 $this->assertEquals(200, $response->getStatusCode());
148 $data = json_decode((string) $response->getBody(), true);
149 $this->assertEquals(self::NB_FIELDS_TAG, count($data));
150 $this->assertEquals($newName, $data['name']);
151 $this->assertEquals(3, $data['occurrences']);
152
153 $tags = $this->linkDB->linksCountPerTag();
154 $this->assertNotTrue(isset($tags[$tagName]));
155 $this->assertEquals(3, $tags[$newName]);
156 }
157
158 /**
159 * Test tag update with an empty new tag name => ApiBadParametersException
160 *
161 * @expectedException Shaarli\Api\Exceptions\ApiBadParametersException
162 * @expectedExceptionMessage New tag name is required in the request body
163 */
164 public function testPutTagEmpty()
165 {
166 $tagName = 'gnu';
167 $newName = '';
168
169 $tags = $this->linkDB->linksCountPerTag();
170 $this->assertEquals(2, $tags[$tagName]);
171
172 $env = Environment::mock([
173 'REQUEST_METHOD' => 'PUT',
174 ]);
175 $request = Request::createFromEnvironment($env);
176
177 $env = Environment::mock([
178 'REQUEST_METHOD' => 'PUT',
179 ]);
180 $update = ['name' => $newName];
181 $request = Request::createFromEnvironment($env);
182 $request = $request->withParsedBody($update);
183
184 try {
185 $this->controller->putTag($request, new Response(), ['tagName' => $tagName]);
186 } catch (ApiBadParametersException $e) {
187 $tags = $this->linkDB->linksCountPerTag();
188 $this->assertEquals(2, $tags[$tagName]);
189 throw $e;
190 }
191 }
192
193 /**
194 * Test tag update on non existent tag => ApiTagNotFoundException.
195 *
196 * @expectedException Shaarli\Api\Exceptions\ApiTagNotFoundException
197 * @expectedExceptionMessage Tag not found
198 */
199 public function testPutTag404()
200 {
201 $env = Environment::mock([
202 'REQUEST_METHOD' => 'PUT',
203 ]);
204 $request = Request::createFromEnvironment($env);
205
206 $this->controller->putTag($request, new Response(), ['tagName' => 'nopenope']);
207 }
208}