2 namespace Shaarli\Api\Controllers
;
4 use Shaarli\Config\ConfigManager
;
7 use Slim\Http\Environment
;
9 use Slim\Http\Response
;
14 * Test REST API controller Info.
16 * @package Api\Controllers
18 class InfoTest
extends \PHPUnit_Framework_TestCase
21 * @var string datastore to test write operations
23 protected static $testDatastore = 'sandbox/datastore.php';
26 * @var ConfigManager instance
31 * @var \ReferenceLinkDB instance.
33 protected $refDB = null;
36 * @var Container instance.
41 * @var Info controller instance.
43 protected $controller;
46 * Before every test, instantiate a new Api with its config, plugins and links.
48 public function setUp()
50 $this->conf
= new ConfigManager('tests/utils/config/configJson.json.php');
51 $this->refDB
= new \
ReferenceLinkDB();
52 $this->refDB
->write(self
::$testDatastore);
54 $this->container
= new Container();
55 $this->container
['conf'] = $this->conf
;
56 $this->container
['db'] = new \
LinkDB(self
::$testDatastore, true, false);
57 $this->container
['history'] = null;
59 $this->controller
= new Info($this->container
);
63 * After every test, remove the test datastore.
65 public function tearDown()
67 @unlink(self
::$testDatastore);
73 public function testGetInfo()
75 $env = Environment
::mock([
76 'REQUEST_METHOD' => 'GET',
78 $request = Request
::createFromEnvironment($env);
80 $response = $this->controller
->getInfo($request, new Response());
81 $this->assertEquals(200, $response->getStatusCode());
82 $data = json_decode((string) $response->getBody(), true);
84 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
85 $this->assertEquals(2, $data['private_counter']);
86 $this->assertEquals('Shaarli', $data['settings']['title']);
87 $this->assertEquals('?', $data['settings']['header_link']);
88 $this->assertEquals('UTC', $data['settings']['timezone']);
89 $this->assertEquals(ConfigManager
::$DEFAULT_PLUGINS, $data['settings']['enabled_plugins']);
90 $this->assertEquals(false, $data['settings']['default_private_links']);
93 $headerLink = 'http://shaarli.tld';
94 $timezone = 'Europe/Paris';
95 $enabledPlugins = array('foo', 'bar');
96 $defaultPrivateLinks = true;
97 $this->conf
->set('general.title', $title);
98 $this->conf
->set('general.header_link', $headerLink);
99 $this->conf
->set('general.timezone', $timezone);
100 $this->conf
->set('general.enabled_plugins', $enabledPlugins);
101 $this->conf
->set('privacy.default_private_links', $defaultPrivateLinks);
103 $response = $this->controller
->getInfo($request, new Response());
104 $this->assertEquals(200, $response->getStatusCode());
105 $data = json_decode((string) $response->getBody(), true);
107 $this->assertEquals(\ReferenceLinkDB
::$NB_LINKS_TOTAL, $data['global_counter']);
108 $this->assertEquals(2, $data['private_counter']);
109 $this->assertEquals($title, $data['settings']['title']);
110 $this->assertEquals($headerLink, $data['settings']['header_link']);
111 $this->assertEquals($timezone, $data['settings']['timezone']);
112 $this->assertEquals($enabledPlugins, $data['settings']['enabled_plugins']);
113 $this->assertEquals($defaultPrivateLinks, $data['settings']['default_private_links']);