InfoTest.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Shaarli\Api\Controllers;
  3. use Slim\Container;
  4. use Slim\Http\Environment;
  5. use Slim\Http\Request;
  6. use Slim\Http\Response;
  7. /**
  8. * Class InfoTest
  9. *
  10. * Test REST API controller Info.
  11. *
  12. * @package Api\Controllers
  13. */
  14. class InfoTest extends \PHPUnit_Framework_TestCase
  15. {
  16. /**
  17. * @var string datastore to test write operations
  18. */
  19. protected static $testDatastore = 'sandbox/datastore.php';
  20. /**
  21. * @var \ConfigManager instance
  22. */
  23. protected $conf;
  24. /**
  25. * @var \ReferenceLinkDB instance.
  26. */
  27. protected $refDB = null;
  28. /**
  29. * @var Container instance.
  30. */
  31. protected $container;
  32. /**
  33. * @var Info controller instance.
  34. */
  35. protected $controller;
  36. /**
  37. * Before every test, instantiate a new Api with its config, plugins and links.
  38. */
  39. public function setUp()
  40. {
  41. $this->conf = new \ConfigManager('tests/utils/config/configJson.json.php');
  42. $this->refDB = new \ReferenceLinkDB();
  43. $this->refDB->write(self::$testDatastore);
  44. $this->container = new Container();
  45. $this->container['conf'] = $this->conf;
  46. $this->container['db'] = new \LinkDB(self::$testDatastore, true, false);
  47. $this->controller = new Info($this->container);
  48. }
  49. /**
  50. * After every test, remove the test datastore.
  51. */
  52. public function tearDown()
  53. {
  54. @unlink(self::$testDatastore);
  55. }
  56. /**
  57. * Test /info service.
  58. */
  59. public function testGetInfo()
  60. {
  61. $env = Environment::mock([
  62. 'REQUEST_METHOD' => 'GET',
  63. ]);
  64. $request = Request::createFromEnvironment($env);
  65. $response = $this->controller->getInfo($request, new Response());
  66. $this->assertEquals(200, $response->getStatusCode());
  67. $data = json_decode((string) $response->getBody(), true);
  68. $this->assertEquals(8, $data['global_counter']);
  69. $this->assertEquals(2, $data['private_counter']);
  70. $this->assertEquals('Shaarli', $data['settings']['title']);
  71. $this->assertEquals('?', $data['settings']['header_link']);
  72. $this->assertEquals('UTC', $data['settings']['timezone']);
  73. $this->assertEquals(\ConfigManager::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
  74. $this->assertEquals(false, $data['settings']['default_private_links']);
  75. $title = 'My links';
  76. $headerLink = 'http://shaarli.tld';
  77. $timezone = 'Europe/Paris';
  78. $enabledPlugins = array('foo', 'bar');
  79. $defaultPrivateLinks = true;
  80. $this->conf->set('general.title', $title);
  81. $this->conf->set('general.header_link', $headerLink);
  82. $this->conf->set('general.timezone', $timezone);
  83. $this->conf->set('general.enabled_plugins', $enabledPlugins);
  84. $this->conf->set('privacy.default_private_links', $defaultPrivateLinks);
  85. $response = $this->controller->getInfo($request, new Response());
  86. $this->assertEquals(200, $response->getStatusCode());
  87. $data = json_decode((string) $response->getBody(), true);
  88. $this->assertEquals(8, $data['global_counter']);
  89. $this->assertEquals(2, $data['private_counter']);
  90. $this->assertEquals($title, $data['settings']['title']);
  91. $this->assertEquals($headerLink, $data['settings']['header_link']);
  92. $this->assertEquals($timezone, $data['settings']['timezone']);
  93. $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
  94. $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);
  95. }
  96. }