InfoTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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->controller = new Info($this->container);
  49. }
  50. /**
  51. * After every test, remove the test datastore.
  52. */
  53. public function tearDown()
  54. {
  55. @unlink(self::$testDatastore);
  56. }
  57. /**
  58. * Test /info service.
  59. */
  60. public function testGetInfo()
  61. {
  62. $env = Environment::mock([
  63. 'REQUEST_METHOD' => 'GET',
  64. ]);
  65. $request = Request::createFromEnvironment($env);
  66. $response = $this->controller->getInfo($request, new Response());
  67. $this->assertEquals(200, $response->getStatusCode());
  68. $data = json_decode((string) $response->getBody(), true);
  69. $this->assertEquals(8, $data['global_counter']);
  70. $this->assertEquals(2, $data['private_counter']);
  71. $this->assertEquals('Shaarli', $data['settings']['title']);
  72. $this->assertEquals('?', $data['settings']['header_link']);
  73. $this->assertEquals('UTC', $data['settings']['timezone']);
  74. $this->assertEquals(ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
  75. $this->assertEquals(false, $data['settings']['default_private_links']);
  76. $title = 'My links';
  77. $headerLink = 'http://shaarli.tld';
  78. $timezone = 'Europe/Paris';
  79. $enabledPlugins = array('foo', 'bar');
  80. $defaultPrivateLinks = true;
  81. $this->conf->set('general.title', $title);
  82. $this->conf->set('general.header_link', $headerLink);
  83. $this->conf->set('general.timezone', $timezone);
  84. $this->conf->set('general.enabled_plugins', $enabledPlugins);
  85. $this->conf->set('privacy.default_private_links', $defaultPrivateLinks);
  86. $response = $this->controller->getInfo($request, new Response());
  87. $this->assertEquals(200, $response->getStatusCode());
  88. $data = json_decode((string) $response->getBody(), true);
  89. $this->assertEquals(8, $data['global_counter']);
  90. $this->assertEquals(2, $data['private_counter']);
  91. $this->assertEquals($title, $data['settings']['title']);
  92. $this->assertEquals($headerLink, $data['settings']['header_link']);
  93. $this->assertEquals($timezone, $data['settings']['timezone']);
  94. $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
  95. $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);
  96. }
  97. }