2015-11-07 16:37:23 +01:00
|
|
|
<?php
|
2020-09-22 12:44:08 +02:00
|
|
|
|
2018-12-04 23:17:23 +01:00
|
|
|
namespace Shaarli\Plugin\Archiveorg;
|
2015-11-07 16:37:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* PluginArchiveorgTest.php
|
|
|
|
*/
|
|
|
|
|
2018-12-04 00:26:50 +01:00
|
|
|
use Shaarli\Plugin\PluginManager;
|
2020-09-29 14:41:40 +02:00
|
|
|
use Shaarli\TestCase;
|
2018-12-04 00:26:50 +01:00
|
|
|
|
2015-11-07 16:37:23 +01:00
|
|
|
require_once 'plugins/archiveorg/archiveorg.php';
|
|
|
|
|
|
|
|
/**
|
2016-10-11 17:37:42 +02:00
|
|
|
* Class PluginArchiveorgTest
|
|
|
|
* Unit test for the archiveorg plugin
|
2015-11-07 16:37:23 +01:00
|
|
|
*/
|
2020-09-22 12:44:08 +02:00
|
|
|
class PluginArchiveorgTest extends TestCase
|
2015-11-07 16:37:23 +01:00
|
|
|
{
|
2020-09-22 12:44:08 +02:00
|
|
|
protected $savedScriptName;
|
|
|
|
|
2015-11-07 16:37:23 +01:00
|
|
|
/**
|
|
|
|
* Reset plugin path
|
|
|
|
*/
|
2020-09-22 12:44:08 +02:00
|
|
|
public function setUp(): void
|
2015-11-07 16:37:23 +01:00
|
|
|
{
|
|
|
|
PluginManager::$PLUGINS_PATH = 'plugins';
|
2020-09-22 12:44:08 +02:00
|
|
|
|
|
|
|
// plugins manipulate global vars
|
|
|
|
$_SERVER['SERVER_PORT'] = '80';
|
|
|
|
$_SERVER['SERVER_NAME'] = 'shaarli.shaarli';
|
|
|
|
$this->savedScriptName = $_SERVER['SCRIPT_NAME'] ?? null;
|
|
|
|
$_SERVER['SCRIPT_NAME'] = '/index.php';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown(): void
|
|
|
|
{
|
|
|
|
unset($_SERVER['SERVER_PORT']);
|
|
|
|
unset($_SERVER['SERVER_NAME']);
|
|
|
|
$_SERVER['SCRIPT_NAME'] = $this->savedScriptName;
|
2015-11-07 16:37:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Test render_linklist hook on external bookmarks.
|
2015-11-07 16:37:23 +01:00
|
|
|
*/
|
2020-09-22 12:44:08 +02:00
|
|
|
public function testArchiveorgLinklistOnExternalLinks(): void
|
2015-11-07 16:37:23 +01:00
|
|
|
{
|
|
|
|
$str = 'http://randomstr.com/test';
|
2016-10-11 17:37:42 +02:00
|
|
|
|
2015-11-07 16:37:23 +01:00
|
|
|
$data = array(
|
|
|
|
'title' => $str,
|
|
|
|
'links' => array(
|
|
|
|
array(
|
|
|
|
'url' => $str,
|
2016-10-11 17:37:42 +02:00
|
|
|
'private' => 0,
|
|
|
|
'real_url' => $str
|
2015-11-07 16:37:23 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = hook_archiveorg_render_linklist($data);
|
2016-10-11 17:37:42 +02:00
|
|
|
|
2015-11-07 16:37:23 +01:00
|
|
|
$link = $data['links'][0];
|
|
|
|
// data shouldn't be altered
|
|
|
|
$this->assertEquals($str, $data['title']);
|
|
|
|
$this->assertEquals($str, $link['url']);
|
|
|
|
|
|
|
|
// plugin data
|
|
|
|
$this->assertEquals(1, count($link['link_plugin']));
|
|
|
|
$this->assertNotFalse(strpos($link['link_plugin'][0], $str));
|
2016-10-11 17:37:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-01-17 21:34:12 +01:00
|
|
|
* Test render_linklist hook on internal bookmarks.
|
2016-10-11 17:37:42 +02:00
|
|
|
*/
|
2020-09-22 12:44:08 +02:00
|
|
|
public function testArchiveorgLinklistOnInternalLinks(): void
|
2016-10-11 17:37:42 +02:00
|
|
|
{
|
2020-09-22 12:44:08 +02:00
|
|
|
$internalLink1 = 'http://shaarli.shaarli/shaare/qvMAqg';
|
|
|
|
$internalLinkRealURL1 = '/shaare/qvMAqg';
|
2016-10-11 17:37:42 +02:00
|
|
|
|
2020-09-22 12:44:08 +02:00
|
|
|
$internalLink2 = 'http://shaarli.shaarli/shaare/2_7zww';
|
|
|
|
$internalLinkRealURL2 = '/shaare/2_7zww';
|
2016-10-11 17:37:42 +02:00
|
|
|
|
2020-09-22 12:44:08 +02:00
|
|
|
$internalLink3 = 'http://shaarli.shaarli/shaare/z7u-_Q';
|
|
|
|
$internalLinkRealURL3 = '/shaare/z7u-_Q';
|
2016-10-11 17:37:42 +02:00
|
|
|
|
|
|
|
$data = array(
|
|
|
|
'title' => $internalLink1,
|
|
|
|
'links' => array(
|
|
|
|
array(
|
|
|
|
'url' => $internalLink1,
|
|
|
|
'private' => 0,
|
|
|
|
'real_url' => $internalLinkRealURL1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'url' => $internalLink1,
|
|
|
|
'private' => 1,
|
|
|
|
'real_url' => $internalLinkRealURL1
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'url' => $internalLink2,
|
|
|
|
'private' => 0,
|
|
|
|
'real_url' => $internalLinkRealURL2
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'url' => $internalLink2,
|
|
|
|
'private' => 1,
|
|
|
|
'real_url' => $internalLinkRealURL2
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'url' => $internalLink3,
|
|
|
|
'private' => 0,
|
|
|
|
'real_url' => $internalLinkRealURL3
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'url' => $internalLink3,
|
|
|
|
'private' => 1,
|
|
|
|
'real_url' => $internalLinkRealURL3
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$data = hook_archiveorg_render_linklist($data);
|
|
|
|
|
|
|
|
// Case n°1: first link type, public
|
|
|
|
$link = $data['links'][0];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($link['link_plugin']));
|
|
|
|
$this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink1));
|
|
|
|
|
|
|
|
// Case n°2: first link type, private
|
|
|
|
$link = $data['links'][1];
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('link_plugin', $link);
|
|
|
|
|
|
|
|
// Case n°3: second link type, public
|
|
|
|
$link = $data['links'][2];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($link['link_plugin']));
|
|
|
|
$this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink2));
|
|
|
|
|
|
|
|
// Case n°4: second link type, private
|
|
|
|
$link = $data['links'][3];
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('link_plugin', $link);
|
|
|
|
|
|
|
|
// Case n°5: third link type, public
|
|
|
|
$link = $data['links'][4];
|
|
|
|
|
|
|
|
$this->assertEquals(1, count($link['link_plugin']));
|
|
|
|
$this->assertNotFalse(strpos($link['link_plugin'][0], $internalLink3));
|
|
|
|
|
|
|
|
// Case n°6: third link type, private
|
|
|
|
$link = $data['links'][5];
|
|
|
|
|
|
|
|
$this->assertArrayNotHasKey('link_plugin', $link);
|
2015-11-07 16:37:23 +01:00
|
|
|
}
|
|
|
|
}
|