2016-11-09 18:57:02 +01:00
|
|
|
<?php
|
|
|
|
|
2017-11-11 14:01:21 +01:00
|
|
|
namespace Shaarli;
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Shaarli\Config\ConfigManager;
|
|
|
|
use WebThumbnailer\Application\ConfigManager as WTConfigManager;
|
2016-11-09 18:57:02 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ThumbnailerTest
|
|
|
|
*
|
|
|
|
* We only make 1 thumb test because:
|
|
|
|
*
|
|
|
|
* 1. the thumbnailer library is itself tested
|
|
|
|
* 2. we don't want to make too many external requests during the tests
|
|
|
|
*/
|
2017-11-11 14:01:21 +01:00
|
|
|
class ThumbnailerTest extends TestCase
|
2016-11-09 18:57:02 +01:00
|
|
|
{
|
2017-11-11 14:01:21 +01:00
|
|
|
const WIDTH = 190;
|
|
|
|
|
|
|
|
const HEIGHT = 210;
|
|
|
|
|
2016-11-09 18:57:02 +01:00
|
|
|
/**
|
2017-11-11 14:01:21 +01:00
|
|
|
* @var Thumbnailer;
|
2016-11-09 18:57:02 +01:00
|
|
|
*/
|
2017-11-11 14:01:21 +01:00
|
|
|
protected $thumbnailer;
|
|
|
|
|
2018-07-05 20:29:55 +02:00
|
|
|
/**
|
|
|
|
* @var ConfigManager
|
|
|
|
*/
|
|
|
|
protected $conf;
|
|
|
|
|
2017-11-11 14:01:21 +01:00
|
|
|
public function setUp()
|
2016-11-09 18:57:02 +01:00
|
|
|
{
|
2018-07-05 20:29:55 +02:00
|
|
|
$this->conf = new ConfigManager('tests/utils/config/configJson');
|
|
|
|
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_ALL);
|
|
|
|
$this->conf->set('thumbnails.width', self::WIDTH);
|
|
|
|
$this->conf->set('thumbnails.height', self::HEIGHT);
|
|
|
|
$this->conf->set('dev.debug', true);
|
2017-11-11 14:01:21 +01:00
|
|
|
|
2018-07-05 20:29:55 +02:00
|
|
|
$this->thumbnailer = new Thumbnailer($this->conf);
|
2017-11-11 14:01:21 +01:00
|
|
|
// cache files in the sandbox
|
|
|
|
WTConfigManager::addFile('tests/utils/config/wt.json');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown()
|
|
|
|
{
|
|
|
|
$this->rrmdirContent('sandbox/');
|
|
|
|
}
|
2016-11-09 18:57:02 +01:00
|
|
|
|
2017-11-11 14:01:21 +01:00
|
|
|
/**
|
2018-07-05 20:29:55 +02:00
|
|
|
* Test a thumbnail with a custom size in 'all' mode.
|
2017-11-11 14:01:21 +01:00
|
|
|
*/
|
2018-07-05 20:29:55 +02:00
|
|
|
public function testThumbnailAllValid()
|
2017-11-11 14:01:21 +01:00
|
|
|
{
|
|
|
|
$thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
|
2016-11-09 18:57:02 +01:00
|
|
|
$this->assertNotFalse($thumb);
|
|
|
|
$image = imagecreatefromstring(file_get_contents($thumb));
|
2017-11-11 14:01:21 +01:00
|
|
|
$this->assertEquals(self::WIDTH, imagesx($image));
|
|
|
|
$this->assertEquals(self::HEIGHT, imagesy($image));
|
2016-11-09 18:57:02 +01:00
|
|
|
}
|
|
|
|
|
2018-07-05 20:29:55 +02:00
|
|
|
/**
|
|
|
|
* Test a thumbnail with a custom size in 'common' mode.
|
|
|
|
*/
|
|
|
|
public function testThumbnailCommonValid()
|
|
|
|
{
|
|
|
|
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
|
|
|
|
$thumb = $this->thumbnailer->get('https://imgur.com/jlFgGpe');
|
|
|
|
$this->assertNotFalse($thumb);
|
|
|
|
$image = imagecreatefromstring(file_get_contents($thumb));
|
|
|
|
$this->assertEquals(self::WIDTH, imagesx($image));
|
|
|
|
$this->assertEquals(self::HEIGHT, imagesy($image));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test a thumbnail in 'common' mode which isn't include in common websites.
|
|
|
|
*/
|
|
|
|
public function testThumbnailCommonInvalid()
|
|
|
|
{
|
|
|
|
$this->conf->set('thumbnails.mode', Thumbnailer::MODE_COMMON);
|
|
|
|
$thumb = $this->thumbnailer->get('https://github.com/shaarli/Shaarli/');
|
|
|
|
$this->assertFalse($thumb);
|
|
|
|
}
|
|
|
|
|
2016-11-09 18:57:02 +01:00
|
|
|
/**
|
|
|
|
* Test a thumbnail that can't be retrieved.
|
|
|
|
*/
|
|
|
|
public function testThumbnailNotValid()
|
|
|
|
{
|
|
|
|
$oldlog = ini_get('error_log');
|
|
|
|
ini_set('error_log', '/dev/null');
|
|
|
|
|
|
|
|
$thumbnailer = new Thumbnailer(new ConfigManager());
|
|
|
|
$thumb = $thumbnailer->get('nope');
|
|
|
|
$this->assertFalse($thumb);
|
|
|
|
|
|
|
|
ini_set('error_log', $oldlog);
|
|
|
|
}
|
2017-11-11 14:01:21 +01:00
|
|
|
|
2018-10-13 00:35:47 +02:00
|
|
|
protected function rrmdirContent($dir)
|
|
|
|
{
|
2017-11-11 14:01:21 +01:00
|
|
|
if (is_dir($dir)) {
|
|
|
|
$objects = scandir($dir);
|
|
|
|
foreach ($objects as $object) {
|
|
|
|
if ($object != "." && $object != "..") {
|
2018-10-13 00:35:47 +02:00
|
|
|
if (is_dir($dir."/".$object)) {
|
2017-11-11 14:01:21 +01:00
|
|
|
$this->rrmdirContent($dir."/".$object);
|
2018-10-13 00:35:47 +02:00
|
|
|
} else {
|
2017-11-11 14:01:21 +01:00
|
|
|
unlink($dir."/".$object);
|
2018-10-13 00:35:47 +02:00
|
|
|
}
|
2017-11-11 14:01:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-11-09 18:57:02 +01:00
|
|
|
}
|