InfoTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 InfoTest
  10. *
  11. * Test REST API controller Info.
  12. *
  13. * @package Api\Controllers
  14. */
  15. class InfoTest 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 Info controller instance.
  35. */
  36. protected $controller;
  37. /**
  38. * Before every test, instantiate a new Api with its config, plugins and links.
  39. */
  40. public function setUp()
  41. {
  42. $this->conf = new ConfigManager('tests/utils/config/configJson.json.php');
  43. $this->refDB = new \ReferenceLinkDB();
  44. $this->refDB->write(self::$testDatastore);
  45. $this->container = new Container();
  46. $this->container['conf'] = $this->conf;
  47. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  48. $this->container['history'] = null;
  49. $this->controller = new Info($this->container);
  50. }
  51. /**
  52. * After every test, remove the test datastore.
  53. */
  54. public function tearDown()
  55. {
  56. @unlink(self::$testDatastore);
  57. }
  58. /**
  59. * Test /info service.
  60. */
  61. public function testGetInfo()
  62. {
  63. $env = Environment::mock([
  64. 'REQUEST_METHOD' => 'GET',
  65. ]);
  66. $request = Request::createFromEnvironment($env);
  67. $response = $this->controller->getInfo($request, new Response());
  68. $this->assertEquals(200, $response->getStatusCode());
  69. $data = json_decode((string) $response->getBody(), true);
  70. $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
  71. $this->assertEquals(2, $data['private_counter']);
  72. $this->assertEquals('Shaarli', $data['settings']['title']);
  73. $this->assertEquals('?', $data['settings']['header_link']);
  74. $this->assertEquals('UTC', $data['settings']['timezone']);
  75. $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
  76. $this->assertEquals(false, $data['settings']['default_private_links']);
  77. $title = 'My links';
  78. $headerLink = 'http://shaarli.tld';
  79. $timezone = 'Europe/Paris';
  80. $enabledPlugins = array('foo', 'bar');
  81. $defaultPrivateLinks = true;
  82. $this->conf->set('general.title', $title);
  83. $this->conf->set('general.header_link', $headerLink);
  84. $this->conf->set('general.timezone', $timezone);
  85. $this->conf->set('general.enabled_plugins', $enabledPlugins);
  86. $this->conf->set('privacy.default_private_links', $defaultPrivateLinks);
  87. $response = $this->controller->getInfo($request, new Response());
  88. $this->assertEquals(200, $response->getStatusCode());
  89. $data = json_decode((string) $response->getBody(), true);
  90. $this->assertEquals(\ReferenceLinkDB::$NB_LINKS_TOTAL, $data['global_counter']);
  91. $this->assertEquals(2, $data['private_counter']);
  92. $this->assertEquals($title, $data['settings']['title']);
  93. $this->assertEquals($headerLink, $data['settings']['header_link']);
  94. $this->assertEquals($timezone, $data['settings']['timezone']);
  95. $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
  96. $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);
  97. }
  98. }