GetTagsTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use Shaarli\Config\ConfigManager;
  4. use Slim\Container;
  5. use Slim\Http\Environment;
  6. use Slim\Http\Request;
  7. use Slim\Http\Response;
  8. /**
  9. * Class GetTagsTest
  10. *
  11. * Test get tag list REST API service.
  12. *
  13. * @package Shaarli\Api\Controllers
  14. */
  15. class GetTagsTest extends \PHPUnit_Framework_TestCase
  16. {
  17. /**
  18. * @var string datastore to test write operations
  19. */
  20. protected static $testDatastore = 'sandbox/datastore.php';
  21. /**
  22. * @var ConfigManager instance
  23. */
  24. protected $conf;
  25. /**
  26. * @var \ReferenceLinkDB instance.
  27. */
  28. protected $refDB = null;
  29. /**
  30. * @var Container instance.
  31. */
  32. protected $container;
  33. /**
  34. * @var \LinkDB instance.
  35. */
  36. protected $linkDB;
  37. /**
  38. * @var Tags controller instance.
  39. */
  40. protected $controller;
  41. /**
  42. * Number of JSON field per link.
  43. */
  44. const NB_FIELDS_TAG = 2;
  45. /**
  46. * Before every test, instantiate a new Api with its config, plugins and links.
  47. */
  48. public function setUp()
  49. {
  50. $this->conf = new ConfigManager('tests/utils/config/configJson');
  51. $this->refDB = new \ReferenceLinkDB();
  52. $this->refDB->write(self::$testDatastore);
  53. $this->container = new Container();
  54. $this->container['conf'] = $this->conf;
  55. $this->linkDB = new \LinkDB(self::$testDatastore, true, false);
  56. $this->container['db'] = $this->linkDB;
  57. $this->container['history'] = null;
  58. $this->controller = new Tags($this->container);
  59. }
  60. /**
  61. * After every test, remove the test datastore.
  62. */
  63. public function tearDown()
  64. {
  65. @unlink(self::$testDatastore);
  66. }
  67. /**
  68. * Test basic getTags service: returns all tags.
  69. */
  70. public function testGetTagsAll()
  71. {
  72. $tags = $this->linkDB->linksCountPerTag();
  73. $env = Environment::mock([
  74. 'REQUEST_METHOD' => 'GET',
  75. ]);
  76. $request = Request::createFromEnvironment($env);
  77. $response = $this->controller->getTags($request, new Response());
  78. $this->assertEquals(200, $response->getStatusCode());
  79. $data = json_decode((string) $response->getBody(), true);
  80. $this->assertEquals(count($tags), count($data));
  81. // Check order
  82. $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
  83. $this->assertEquals('web', $data[0]['name']);
  84. $this->assertEquals(4, $data[0]['occurrences']);
  85. $this->assertEquals(self::NB_FIELDS_TAG, count($data[1]));
  86. $this->assertEquals('cartoon', $data[1]['name']);
  87. $this->assertEquals(3, $data[1]['occurrences']);
  88. // Case insensitive
  89. $this->assertEquals(self::NB_FIELDS_TAG, count($data[5]));
  90. $this->assertEquals('sTuff', $data[5]['name']);
  91. $this->assertEquals(2, $data[5]['occurrences']);
  92. // End
  93. $this->assertEquals(self::NB_FIELDS_TAG, count($data[count($data) - 1]));
  94. $this->assertEquals('w3c', $data[count($data) - 1]['name']);
  95. $this->assertEquals(1, $data[count($data) - 1]['occurrences']);
  96. }
  97. /**
  98. * Test getTags service with offset and limit parameter:
  99. * limit=1 and offset=1 should return only the second tag, cartoon with 3 occurrences
  100. */
  101. public function testGetTagsOffsetLimit()
  102. {
  103. $env = Environment::mock([
  104. 'REQUEST_METHOD' => 'GET',
  105. 'QUERY_STRING' => 'offset=1&limit=1'
  106. ]);
  107. $request = Request::createFromEnvironment($env);
  108. $response = $this->controller->getTags($request, new Response());
  109. $this->assertEquals(200, $response->getStatusCode());
  110. $data = json_decode((string) $response->getBody(), true);
  111. $this->assertEquals(1, count($data));
  112. $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
  113. $this->assertEquals('cartoon', $data[0]['name']);
  114. $this->assertEquals(3, $data[0]['occurrences']);
  115. }
  116. /**
  117. * Test getTags with limit=all (return all tags).
  118. */
  119. public function testGetTagsLimitAll()
  120. {
  121. $tags = $this->linkDB->linksCountPerTag();
  122. $env = Environment::mock([
  123. 'REQUEST_METHOD' => 'GET',
  124. 'QUERY_STRING' => 'limit=all'
  125. ]);
  126. $request = Request::createFromEnvironment($env);
  127. $response = $this->controller->getTags($request, new Response());
  128. $this->assertEquals(200, $response->getStatusCode());
  129. $data = json_decode((string) $response->getBody(), true);
  130. $this->assertEquals(count($tags), count($data));
  131. }
  132. /**
  133. * Test getTags service with offset and limit parameter:
  134. * limit=1 and offset=1 should not return any tag
  135. */
  136. public function testGetTagsOffsetTooHigh()
  137. {
  138. $env = Environment::mock([
  139. 'REQUEST_METHOD' => 'GET',
  140. 'QUERY_STRING' => 'offset=100'
  141. ]);
  142. $request = Request::createFromEnvironment($env);
  143. $response = $this->controller->getTags($request, new Response());
  144. $this->assertEquals(200, $response->getStatusCode());
  145. $data = json_decode((string) $response->getBody(), true);
  146. $this->assertEmpty(count($data));
  147. }
  148. /**
  149. * Test getTags with visibility parameter set to private
  150. */
  151. public function testGetTagsVisibilityPrivate()
  152. {
  153. $tags = $this->linkDB->linksCountPerTag([], 'private');
  154. $env = Environment::mock([
  155. 'REQUEST_METHOD' => 'GET',
  156. 'QUERY_STRING' => 'visibility=private'
  157. ]);
  158. $request = Request::createFromEnvironment($env);
  159. $response = $this->controller->getTags($request, new Response());
  160. $this->assertEquals(200, $response->getStatusCode());
  161. $data = json_decode((string) $response->getBody(), true);
  162. $this->assertEquals(count($tags), count($data));
  163. $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
  164. $this->assertEquals('Mercurial', $data[0]['name']);
  165. $this->assertEquals(1, $data[0]['occurrences']);
  166. }
  167. /**
  168. * Test getTags with visibility parameter set to public
  169. */
  170. public function testGetTagsVisibilityPublic()
  171. {
  172. $tags = $this->linkDB->linksCountPerTag([], 'public');
  173. $env = Environment::mock(
  174. [
  175. 'REQUEST_METHOD' => 'GET',
  176. 'QUERY_STRING' => 'visibility=public'
  177. ]
  178. );
  179. $request = Request::createFromEnvironment($env);
  180. $response = $this->controller->getTags($request, new Response());
  181. $this->assertEquals(200, $response->getStatusCode());
  182. $data = json_decode((string)$response->getBody(), true);
  183. $this->assertEquals(count($tags), count($data));
  184. $this->assertEquals(self::NB_FIELDS_TAG, count($data[0]));
  185. $this->assertEquals('web', $data[0]['name']);
  186. $this->assertEquals(3, $data[0]['occurrences']);
  187. }
  188. }