]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - tests/plugins/PluginIssoTest.php
Compatibility with PHPUnit 9
[github/shaarli/Shaarli.git] / tests / plugins / PluginIssoTest.php
1 <?php
2 namespace Shaarli\Plugin\Isso;
3
4 use DateTime;
5 use Shaarli\Bookmark\Bookmark;
6 use Shaarli\Config\ConfigManager;
7 use Shaarli\Plugin\PluginManager;
8 use Shaarli\TestCase;
9
10 require_once 'plugins/isso/isso.php';
11
12 /**
13 * Class PluginIssoTest
14 *
15 * Test the Isso plugin (comment system).
16 */
17 class PluginIssoTest extends TestCase
18 {
19 /**
20 * Reset plugin path
21 */
22 public function setUp(): void
23 {
24 PluginManager::$PLUGINS_PATH = 'plugins';
25 }
26
27 /**
28 * Test Isso init without errors.
29 */
30 public function testIssoInitNoError(): void
31 {
32 $conf = new ConfigManager('');
33 $conf->set('plugins.ISSO_SERVER', 'value');
34 $errors = isso_init($conf);
35 $this->assertEmpty($errors);
36 }
37
38 /**
39 * Test Isso init with errors.
40 */
41 public function testIssoInitError(): void
42 {
43 $conf = new ConfigManager('');
44 $errors = isso_init($conf);
45 $this->assertNotEmpty($errors);
46 }
47
48 /**
49 * Test render_linklist hook with valid settings to display the comment form.
50 */
51 public function testIssoDisplayed(): void
52 {
53 $conf = new ConfigManager('');
54 $conf->set('plugins.ISSO_SERVER', 'value');
55
56 $str = 'http://randomstr.com/test';
57 $date = '20161118_100001';
58 $data = array(
59 'title' => $str,
60 'links' => array(
61 array(
62 'id' => 12,
63 'url' => $str,
64 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date),
65 )
66 )
67 );
68
69 $data = hook_isso_render_linklist($data, $conf);
70
71 // data shouldn't be altered
72 $this->assertEquals($str, $data['title']);
73 $this->assertEquals($str, $data['links'][0]['url']);
74
75 // plugin data
76 $this->assertEquals(1, count($data['plugin_end_zone']));
77 $this->assertNotFalse(strpos(
78 $data['plugin_end_zone'][0],
79 'data-isso-id="'. $data['links'][0]['id'] .'"'
80 ));
81 $this->assertNotFalse(strpos(
82 $data['plugin_end_zone'][0],
83 'data-title="'. $data['links'][0]['id'] .'"'
84 ));
85 $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js'));
86 }
87
88 /**
89 * Test isso plugin when multiple bookmarks are displayed (shouldn't be displayed).
90 */
91 public function testIssoMultipleLinks(): void
92 {
93 $conf = new ConfigManager('');
94 $conf->set('plugins.ISSO_SERVER', 'value');
95
96 $str = 'http://randomstr.com/test';
97 $date1 = '20161118_100001';
98 $date2 = '20161118_100002';
99 $data = array(
100 'title' => $str,
101 'links' => array(
102 array(
103 'id' => 12,
104 'url' => $str,
105 'shorturl' => $short1 = 'abcd',
106 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date1),
107 ),
108 array(
109 'id' => 13,
110 'url' => $str . '2',
111 'shorturl' => $short2 = 'efgh',
112 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date2),
113 ),
114 )
115 );
116
117 $processed = hook_isso_render_linklist($data, $conf);
118 // link_plugin should be added for the icon
119 $this->assertContainsPolyfill('<a href="/shaare/'. $short1 .'#isso-thread">', $processed['links'][0]['link_plugin'][0]);
120 $this->assertContainsPolyfill('<a href="/shaare/'. $short2 .'#isso-thread">', $processed['links'][1]['link_plugin'][0]);
121 }
122
123 /**
124 * Test isso plugin when using search (shouldn't be displayed).
125 */
126 public function testIssoNotDisplayedWhenSearch(): void
127 {
128 $conf = new ConfigManager('');
129 $conf->set('plugins.ISSO_SERVER', 'value');
130
131 $str = 'http://randomstr.com/test';
132 $date = '20161118_100001';
133 $data = array(
134 'title' => $str,
135 'links' => array(
136 array(
137 'id' => 12,
138 'url' => $str,
139 'shorturl' => $short1 = 'abcd',
140 'created' => DateTime::createFromFormat(Bookmark::LINK_DATE_FORMAT, $date),
141 )
142 ),
143 'search_term' => $str
144 );
145
146 $processed = hook_isso_render_linklist($data, $conf);
147
148 // link_plugin should be added for the icon
149 $this->assertContainsPolyfill('<a href="/shaare/'. $short1 .'#isso-thread">', $processed['links'][0]['link_plugin'][0]);
150 }
151
152 /**
153 * Test isso plugin without server configuration (shouldn't be displayed).
154 */
155 public function testIssoWithoutConf(): void
156 {
157 $data = 'abc';
158 $conf = new ConfigManager('');
159 $processed = hook_isso_render_linklist($data, $conf);
160 $this->assertEquals($data, $processed);
161 }
162 }