diff options
Diffstat (limited to 'tests/api/controllers')
-rw-r--r-- | tests/api/controllers/history/HistoryTest.php (renamed from tests/api/controllers/HistoryTest.php) | 1 | ||||
-rw-r--r-- | tests/api/controllers/info/InfoTest.php (renamed from tests/api/controllers/InfoTest.php) | 4 | ||||
-rw-r--r-- | tests/api/controllers/links/DeleteLinkTest.php (renamed from tests/api/controllers/DeleteLinkTest.php) | 0 | ||||
-rw-r--r-- | tests/api/controllers/links/GetLinkIdTest.php (renamed from tests/api/controllers/GetLinkIdTest.php) | 0 | ||||
-rw-r--r-- | tests/api/controllers/links/GetLinksTest.php (renamed from tests/api/controllers/GetLinksTest.php) | 21 | ||||
-rw-r--r-- | tests/api/controllers/links/PostLinkTest.php (renamed from tests/api/controllers/PostLinkTest.php) | 15 | ||||
-rw-r--r-- | tests/api/controllers/links/PutLinkTest.php (renamed from tests/api/controllers/PutLinkTest.php) | 9 | ||||
-rw-r--r-- | tests/api/controllers/tags/DeleteTagTest.php | 164 | ||||
-rw-r--r-- | tests/api/controllers/tags/GetTagNameTest.php | 129 | ||||
-rw-r--r-- | tests/api/controllers/tags/GetTagsTest.php | 209 | ||||
-rw-r--r-- | tests/api/controllers/tags/PutTagTest.php | 208 |
11 files changed, 740 insertions, 20 deletions
diff --git a/tests/api/controllers/HistoryTest.php b/tests/api/controllers/history/HistoryTest.php index 61046d97..ff34e16d 100644 --- a/tests/api/controllers/HistoryTest.php +++ b/tests/api/controllers/history/HistoryTest.php | |||
@@ -3,7 +3,6 @@ | |||
3 | 3 | ||
4 | namespace Shaarli\Api\Controllers; | 4 | namespace Shaarli\Api\Controllers; |
5 | 5 | ||
6 | |||
7 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
8 | use Slim\Container; | 7 | use Slim\Container; |
9 | use Slim\Http\Environment; | 8 | use Slim\Http\Environment; |
diff --git a/tests/api/controllers/InfoTest.php b/tests/api/controllers/info/InfoTest.php index f7e63bfa..e437082a 100644 --- a/tests/api/controllers/InfoTest.php +++ b/tests/api/controllers/info/InfoTest.php | |||
@@ -10,9 +10,9 @@ use Slim\Http\Response; | |||
10 | 10 | ||
11 | /** | 11 | /** |
12 | * Class InfoTest | 12 | * Class InfoTest |
13 | * | 13 | * |
14 | * Test REST API controller Info. | 14 | * Test REST API controller Info. |
15 | * | 15 | * |
16 | * @package Api\Controllers | 16 | * @package Api\Controllers |
17 | */ | 17 | */ |
18 | class InfoTest extends \PHPUnit_Framework_TestCase | 18 | class InfoTest extends \PHPUnit_Framework_TestCase |
diff --git a/tests/api/controllers/DeleteLinkTest.php b/tests/api/controllers/links/DeleteLinkTest.php index 7d797137..7d797137 100644 --- a/tests/api/controllers/DeleteLinkTest.php +++ b/tests/api/controllers/links/DeleteLinkTest.php | |||
diff --git a/tests/api/controllers/GetLinkIdTest.php b/tests/api/controllers/links/GetLinkIdTest.php index 57528d5a..57528d5a 100644 --- a/tests/api/controllers/GetLinkIdTest.php +++ b/tests/api/controllers/links/GetLinkIdTest.php | |||
diff --git a/tests/api/controllers/GetLinksTest.php b/tests/api/controllers/links/GetLinksTest.php index d22ed3bf..64f02774 100644 --- a/tests/api/controllers/GetLinksTest.php +++ b/tests/api/controllers/links/GetLinksTest.php | |||
@@ -95,7 +95,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
95 | $this->assertEquals($this->refDB->countLinks(), count($data)); | 95 | $this->assertEquals($this->refDB->countLinks(), count($data)); |
96 | 96 | ||
97 | // Check order | 97 | // Check order |
98 | $order = [41, 8, 6, 7, 0, 1, 9, 4, 42]; | 98 | $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42]; |
99 | $cpt = 0; | 99 | $cpt = 0; |
100 | foreach ($data as $link) { | 100 | foreach ($data as $link) { |
101 | $this->assertEquals(self::NB_FIELDS_LINK, count($link)); | 101 | $this->assertEquals(self::NB_FIELDS_LINK, count($link)); |
@@ -103,7 +103,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
103 | } | 103 | } |
104 | 104 | ||
105 | // Check first element fields | 105 | // Check first element fields |
106 | $first = $data[0]; | 106 | $first = $data[2]; |
107 | $this->assertEquals('http://domain.tld/?WDWyig', $first['url']); | 107 | $this->assertEquals('http://domain.tld/?WDWyig', $first['url']); |
108 | $this->assertEquals('WDWyig', $first['shorturl']); | 108 | $this->assertEquals('WDWyig', $first['shorturl']); |
109 | $this->assertEquals('Link title: @website', $first['title']); | 109 | $this->assertEquals('Link title: @website', $first['title']); |
@@ -120,7 +120,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
120 | $this->assertEmpty($first['updated']); | 120 | $this->assertEmpty($first['updated']); |
121 | 121 | ||
122 | // Multi tags | 122 | // Multi tags |
123 | $link = $data[1]; | 123 | $link = $data[3]; |
124 | $this->assertEquals(7, count($link['tags'])); | 124 | $this->assertEquals(7, count($link['tags'])); |
125 | 125 | ||
126 | // Update date | 126 | // Update date |
@@ -138,7 +138,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
138 | { | 138 | { |
139 | $env = Environment::mock([ | 139 | $env = Environment::mock([ |
140 | 'REQUEST_METHOD' => 'GET', | 140 | 'REQUEST_METHOD' => 'GET', |
141 | 'QUERY_STRING' => 'offset=1&limit=1' | 141 | 'QUERY_STRING' => 'offset=3&limit=1' |
142 | ]); | 142 | ]); |
143 | $request = Request::createFromEnvironment($env); | 143 | $request = Request::createFromEnvironment($env); |
144 | $response = $this->controller->getLinks($request, new Response()); | 144 | $response = $this->controller->getLinks($request, new Response()); |
@@ -164,7 +164,7 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
164 | $data = json_decode((string) $response->getBody(), true); | 164 | $data = json_decode((string) $response->getBody(), true); |
165 | $this->assertEquals($this->refDB->countLinks(), count($data)); | 165 | $this->assertEquals($this->refDB->countLinks(), count($data)); |
166 | // Check order | 166 | // Check order |
167 | $order = [41, 8, 6, 7, 0, 1, 9, 4, 42]; | 167 | $order = [10, 11, 41, 8, 6, 7, 0, 1, 9, 4, 42]; |
168 | $cpt = 0; | 168 | $cpt = 0; |
169 | foreach ($data as $link) { | 169 | foreach ($data as $link) { |
170 | $this->assertEquals(self::NB_FIELDS_LINK, count($link)); | 170 | $this->assertEquals(self::NB_FIELDS_LINK, count($link)); |
@@ -205,7 +205,8 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
205 | $this->assertEquals(200, $response->getStatusCode()); | 205 | $this->assertEquals(200, $response->getStatusCode()); |
206 | $data = json_decode((string)$response->getBody(), true); | 206 | $data = json_decode((string)$response->getBody(), true); |
207 | $this->assertEquals($this->refDB->countLinks(), count($data)); | 207 | $this->assertEquals($this->refDB->countLinks(), count($data)); |
208 | $this->assertEquals(41, $data[0]['id']); | 208 | $this->assertEquals(10, $data[0]['id']); |
209 | $this->assertEquals(41, $data[2]['id']); | ||
209 | $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); | 210 | $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); |
210 | } | 211 | } |
211 | 212 | ||
@@ -243,7 +244,8 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
243 | $this->assertEquals(200, $response->getStatusCode()); | 244 | $this->assertEquals(200, $response->getStatusCode()); |
244 | $data = json_decode((string)$response->getBody(), true); | 245 | $data = json_decode((string)$response->getBody(), true); |
245 | $this->assertEquals($this->refDB->countPublicLinks(), count($data)); | 246 | $this->assertEquals($this->refDB->countPublicLinks(), count($data)); |
246 | $this->assertEquals(41, $data[0]['id']); | 247 | $this->assertEquals(10, $data[0]['id']); |
248 | $this->assertEquals(41, $data[2]['id']); | ||
247 | $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); | 249 | $this->assertEquals(self::NB_FIELDS_LINK, count($data[0])); |
248 | } | 250 | } |
249 | 251 | ||
@@ -413,8 +415,9 @@ class GetLinksTest extends \PHPUnit_Framework_TestCase | |||
413 | $response = $this->controller->getLinks($request, new Response()); | 415 | $response = $this->controller->getLinks($request, new Response()); |
414 | $this->assertEquals(200, $response->getStatusCode()); | 416 | $this->assertEquals(200, $response->getStatusCode()); |
415 | $data = json_decode((string) $response->getBody(), true); | 417 | $data = json_decode((string) $response->getBody(), true); |
416 | $this->assertEquals(9, count($data)); | 418 | $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, count($data)); |
417 | $this->assertEquals(41, $data[0]['id']); | 419 | $this->assertEquals(10, $data[0]['id']); |
420 | $this->assertEquals(41, $data[2]['id']); | ||
418 | 421 | ||
419 | // wildcard: optional ('*' does not need to expand) | 422 | // wildcard: optional ('*' does not need to expand) |
420 | $env = Environment::mock([ | 423 | $env = Environment::mock([ |
diff --git a/tests/api/controllers/PostLinkTest.php b/tests/api/controllers/links/PostLinkTest.php index 31954e39..5c2b5623 100644 --- a/tests/api/controllers/PostLinkTest.php +++ b/tests/api/controllers/links/PostLinkTest.php | |||
@@ -2,12 +2,13 @@ | |||
2 | 2 | ||
3 | namespace Shaarli\Api\Controllers; | 3 | namespace Shaarli\Api\Controllers; |
4 | 4 | ||
5 | 5 | use PHPUnit\Framework\TestCase; | |
6 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
7 | use Slim\Container; | 7 | use Slim\Container; |
8 | use Slim\Http\Environment; | 8 | use Slim\Http\Environment; |
9 | use Slim\Http\Request; | 9 | use Slim\Http\Request; |
10 | use Slim\Http\Response; | 10 | use Slim\Http\Response; |
11 | use Slim\Router; | ||
11 | 12 | ||
12 | /** | 13 | /** |
13 | * Class PostLinkTest | 14 | * Class PostLinkTest |
@@ -16,7 +17,7 @@ use Slim\Http\Response; | |||
16 | * | 17 | * |
17 | * @package Shaarli\Api\Controllers | 18 | * @package Shaarli\Api\Controllers |
18 | */ | 19 | */ |
19 | class PostLinkTest extends \PHPUnit_Framework_TestCase | 20 | class PostLinkTest extends TestCase |
20 | { | 21 | { |
21 | /** | 22 | /** |
22 | * @var string datastore to test write operations | 23 | * @var string datastore to test write operations |
@@ -78,7 +79,7 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase | |||
78 | 79 | ||
79 | $this->controller = new Links($this->container); | 80 | $this->controller = new Links($this->container); |
80 | 81 | ||
81 | $mock = $this->getMock('\Slim\Router', ['relativePathFor']); | 82 | $mock = $this->createMock(Router::class); |
82 | $mock->expects($this->any()) | 83 | $mock->expects($this->any()) |
83 | ->method('relativePathFor') | 84 | ->method('relativePathFor') |
84 | ->willReturn('api/v1/links/1'); | 85 | ->willReturn('api/v1/links/1'); |
@@ -126,7 +127,9 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase | |||
126 | $this->assertEquals('', $data['description']); | 127 | $this->assertEquals('', $data['description']); |
127 | $this->assertEquals([], $data['tags']); | 128 | $this->assertEquals([], $data['tags']); |
128 | $this->assertEquals(false, $data['private']); | 129 | $this->assertEquals(false, $data['private']); |
129 | $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])); | 130 | $this->assertTrue( |
131 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
132 | ); | ||
130 | $this->assertEquals('', $data['updated']); | 133 | $this->assertEquals('', $data['updated']); |
131 | 134 | ||
132 | $historyEntry = $this->history->getHistory()[0]; | 135 | $historyEntry = $this->history->getHistory()[0]; |
@@ -169,7 +172,9 @@ class PostLinkTest extends \PHPUnit_Framework_TestCase | |||
169 | $this->assertEquals($link['description'], $data['description']); | 172 | $this->assertEquals($link['description'], $data['description']); |
170 | $this->assertEquals($link['tags'], $data['tags']); | 173 | $this->assertEquals($link['tags'], $data['tags']); |
171 | $this->assertEquals(true, $data['private']); | 174 | $this->assertEquals(true, $data['private']); |
172 | $this->assertTrue(new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created'])); | 175 | $this->assertTrue( |
176 | new \DateTime('2 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | ||
177 | ); | ||
173 | $this->assertEquals('', $data['updated']); | 178 | $this->assertEquals('', $data['updated']); |
174 | } | 179 | } |
175 | 180 | ||
diff --git a/tests/api/controllers/PutLinkTest.php b/tests/api/controllers/links/PutLinkTest.php index 8a562571..f276b4c1 100644 --- a/tests/api/controllers/PutLinkTest.php +++ b/tests/api/controllers/links/PutLinkTest.php | |||
@@ -3,7 +3,6 @@ | |||
3 | 3 | ||
4 | namespace Shaarli\Api\Controllers; | 4 | namespace Shaarli\Api\Controllers; |
5 | 5 | ||
6 | |||
7 | use Shaarli\Config\ConfigManager; | 6 | use Shaarli\Config\ConfigManager; |
8 | use Slim\Container; | 7 | use Slim\Container; |
9 | use Slim\Http\Environment; | 8 | use Slim\Http\Environment; |
@@ -115,7 +114,9 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase | |||
115 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | 114 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), |
116 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 115 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
117 | ); | 116 | ); |
118 | $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); | 117 | $this->assertTrue( |
118 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
119 | ); | ||
119 | 120 | ||
120 | $historyEntry = $this->history->getHistory()[0]; | 121 | $historyEntry = $this->history->getHistory()[0]; |
121 | $this->assertEquals(\History::UPDATED, $historyEntry['event']); | 122 | $this->assertEquals(\History::UPDATED, $historyEntry['event']); |
@@ -160,7 +161,9 @@ class PutLinkTest extends \PHPUnit_Framework_TestCase | |||
160 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), | 161 | \DateTime::createFromFormat('Ymd_His', '20150310_114651'), |
161 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) | 162 | \DateTime::createFromFormat(\DateTime::ATOM, $data['created']) |
162 | ); | 163 | ); |
163 | $this->assertTrue(new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated'])); | 164 | $this->assertTrue( |
165 | new \DateTime('5 seconds ago') < \DateTime::createFromFormat(\DateTime::ATOM, $data['updated']) | ||
166 | ); | ||
164 | } | 167 | } |
165 | 168 | ||
166 | /** | 169 | /** |
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 | |||
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 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 | |||
3 | namespace Shaarli\Api\Controllers; | ||
4 | |||
5 | use Shaarli\Config\ConfigManager; | ||
6 | |||
7 | use Slim\Container; | ||
8 | use Slim\Http\Environment; | ||
9 | use Slim\Http\Request; | ||
10 | use Slim\Http\Response; | ||
11 | |||
12 | /** | ||
13 | * Class GetTagNameTest | ||
14 | * | ||
15 | * Test getTag by tag name API service. | ||
16 | * | ||
17 | * @package Shaarli\Api\Controllers | ||
18 | */ | ||
19 | class 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 | ||
2 | namespace Shaarli\Api\Controllers; | ||
3 | |||
4 | use Shaarli\Config\ConfigManager; | ||
5 | |||
6 | use Slim\Container; | ||
7 | use Slim\Http\Environment; | ||
8 | use Slim\Http\Request; | ||
9 | use Slim\Http\Response; | ||
10 | |||
11 | /** | ||
12 | * Class GetTagsTest | ||
13 | * | ||
14 | * Test get tag list REST API service. | ||
15 | * | ||
16 | * @package Shaarli\Api\Controllers | ||
17 | */ | ||
18 | class 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 | |||
4 | namespace Shaarli\Api\Controllers; | ||
5 | |||
6 | use Shaarli\Api\Exceptions\ApiBadParametersException; | ||
7 | use Shaarli\Config\ConfigManager; | ||
8 | use Slim\Container; | ||
9 | use Slim\Http\Environment; | ||
10 | use Slim\Http\Request; | ||
11 | use Slim\Http\Response; | ||
12 | |||
13 | class 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 | } | ||