2016-12-24 10:30:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Shaarli\Api\Controllers;
|
|
|
|
|
2020-09-26 14:18:01 +02:00
|
|
|
use malkusch\lock\mutex\NoMutex;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\Bookmark\BookmarkFileService;
|
2019-01-12 23:55:38 +01:00
|
|
|
use Shaarli\Bookmark\LinkDB;
|
2017-03-03 23:06:12 +01:00
|
|
|
use Shaarli\Config\ConfigManager;
|
2020-01-17 21:34:12 +01:00
|
|
|
use Shaarli\History;
|
2021-01-20 15:59:00 +01:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2016-12-24 10:30:21 +01:00
|
|
|
use Slim\Container;
|
|
|
|
use Slim\Http\Environment;
|
|
|
|
use Slim\Http\Request;
|
|
|
|
use Slim\Http\Response;
|
|
|
|
|
|
|
|
/**
|
2018-05-19 15:04:04 +02:00
|
|
|
* Class GetTagNameTest
|
2016-12-24 10:30:21 +01:00
|
|
|
*
|
2018-05-19 15:04:04 +02:00
|
|
|
* Test getTag by tag name API service.
|
2016-12-24 10:30:21 +01:00
|
|
|
*
|
|
|
|
* @package Shaarli\Api\Controllers
|
|
|
|
*/
|
2020-09-29 14:41:40 +02:00
|
|
|
class GetTagNameTest extends \Shaarli\TestCase
|
2016-12-24 10:30:21 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var string datastore to test write operations
|
|
|
|
*/
|
|
|
|
protected static $testDatastore = 'sandbox/datastore.php';
|
|
|
|
|
|
|
|
/**
|
2017-03-03 23:06:12 +01:00
|
|
|
* @var ConfigManager instance
|
2016-12-24 10:30:21 +01:00
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \ReferenceLinkDB instance.
|
|
|
|
*/
|
|
|
|
protected $refDB = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Container instance.
|
|
|
|
*/
|
|
|
|
protected $container;
|
|
|
|
|
|
|
|
/**
|
2018-05-19 15:04:04 +02:00
|
|
|
* @var Tags controller instance.
|
2016-12-24 10:30:21 +01:00
|
|
|
*/
|
|
|
|
protected $controller;
|
|
|
|
|
2021-01-20 15:59:00 +01:00
|
|
|
/** @var PluginManager */
|
|
|
|
protected $pluginManager;
|
|
|
|
|
2016-12-24 10:30:21 +01:00
|
|
|
/**
|
|
|
|
* Number of JSON fields per link.
|
|
|
|
*/
|
2018-05-19 15:04:04 +02:00
|
|
|
const NB_FIELDS_TAG = 2;
|
2016-12-24 10:30:21 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Before each test, instantiate a new Api with its config, plugins and bookmarks.
|
2016-12-24 10:30:21 +01:00
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function setUp(): void
|
2016-12-24 10:30:21 +01:00
|
|
|
{
|
2020-09-26 14:18:01 +02:00
|
|
|
$mutex = new NoMutex();
|
2017-03-03 23:06:12 +01:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
2020-01-17 21:34:12 +01:00
|
|
|
$this->conf->set('resource.datastore', self::$testDatastore);
|
2016-12-24 10:30:21 +01:00
|
|
|
$this->refDB = new \ReferenceLinkDB();
|
|
|
|
$this->refDB->write(self::$testDatastore);
|
2020-01-17 21:34:12 +01:00
|
|
|
$history = new History('sandbox/history.php');
|
2016-12-24 10:30:21 +01:00
|
|
|
|
|
|
|
$this->container = new Container();
|
|
|
|
$this->container['conf'] = $this->conf;
|
2021-01-20 15:59:00 +01:00
|
|
|
$this->pluginManager = new PluginManager($this->conf);
|
|
|
|
$this->container['db'] = new BookmarkFileService(
|
|
|
|
$this->conf,
|
|
|
|
$this->pluginManager,
|
|
|
|
$history,
|
|
|
|
$mutex,
|
|
|
|
true
|
|
|
|
);
|
2017-05-07 16:50:20 +02:00
|
|
|
$this->container['history'] = null;
|
2016-12-24 10:30:21 +01:00
|
|
|
|
2018-05-19 15:04:04 +02:00
|
|
|
$this->controller = new Tags($this->container);
|
2016-12-24 10:30:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After each test, remove the test datastore.
|
|
|
|
*/
|
2020-09-26 15:08:39 +02:00
|
|
|
protected function tearDown(): void
|
2016-12-24 10:30:21 +01:00
|
|
|
{
|
|
|
|
@unlink(self::$testDatastore);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-05-19 15:04:04 +02:00
|
|
|
* Test basic getTag service: return gnu tag with 2 occurrences.
|
2016-12-24 10:30:21 +01:00
|
|
|
*/
|
2018-05-19 15:04:04 +02:00
|
|
|
public function testGetTag()
|
2016-12-24 10:30:21 +01:00
|
|
|
{
|
2018-05-19 15:04:04 +02:00
|
|
|
$tagName = 'gnu';
|
|
|
|
$env = Environment::mock([
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
]);
|
|
|
|
$request = Request::createFromEnvironment($env);
|
|
|
|
|
|
|
|
$response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$data = json_decode((string) $response->getBody(), true);
|
|
|
|
$this->assertEquals(self::NB_FIELDS_TAG, count($data));
|
|
|
|
$this->assertEquals($tagName, $data['name']);
|
|
|
|
$this->assertEquals(2, $data['occurrences']);
|
|
|
|
}
|
2016-12-24 10:30:21 +01:00
|
|
|
|
2018-05-19 15:04:04 +02:00
|
|
|
/**
|
|
|
|
* Test getTag service which is not case sensitive: occurrences with both sTuff and stuff
|
|
|
|
*/
|
|
|
|
public function testGetTagNotCaseSensitive()
|
|
|
|
{
|
|
|
|
$tagName = 'sTuff';
|
2016-12-24 10:30:21 +01:00
|
|
|
$env = Environment::mock([
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
]);
|
|
|
|
$request = Request::createFromEnvironment($env);
|
|
|
|
|
2018-05-19 15:04:04 +02:00
|
|
|
$response = $this->controller->getTag($request, new Response(), ['tagName' => $tagName]);
|
2016-12-24 10:30:21 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$data = json_decode((string) $response->getBody(), true);
|
2018-05-19 15:04:04 +02:00
|
|
|
$this->assertEquals(self::NB_FIELDS_TAG, count($data));
|
|
|
|
$this->assertEquals($tagName, $data['name']);
|
|
|
|
$this->assertEquals(2, $data['occurrences']);
|
2016-12-24 10:30:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-06-04 18:58:59 +02:00
|
|
|
* Test basic getTag service: get non existent tag => ApiTagNotFoundException.
|
2016-12-24 10:30:21 +01:00
|
|
|
*/
|
2018-05-19 15:04:04 +02:00
|
|
|
public function testGetTag404()
|
2016-12-24 10:30:21 +01:00
|
|
|
{
|
2020-09-27 14:07:08 +02:00
|
|
|
$this->expectException(\Shaarli\Api\Exceptions\ApiTagNotFoundException::class);
|
|
|
|
$this->expectExceptionMessage('Tag not found');
|
|
|
|
|
2016-12-24 10:30:21 +01:00
|
|
|
$env = Environment::mock([
|
|
|
|
'REQUEST_METHOD' => 'GET',
|
|
|
|
]);
|
|
|
|
$request = Request::createFromEnvironment($env);
|
|
|
|
|
2018-05-19 15:04:04 +02:00
|
|
|
$this->controller->getTag($request, new Response(), ['tagName' => 'nopenope']);
|
2016-12-24 10:30:21 +01:00
|
|
|
}
|
|
|
|
}
|