aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/container/ContainerBuilderTest.php2
-rw-r--r--tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php118
-rw-r--r--tests/http/MetadataRetrieverTest.php123
3 files changed, 208 insertions, 35 deletions
diff --git a/tests/container/ContainerBuilderTest.php b/tests/container/ContainerBuilderTest.php
index 5d52daef..3dadc0b9 100644
--- a/tests/container/ContainerBuilderTest.php
+++ b/tests/container/ContainerBuilderTest.php
@@ -12,6 +12,7 @@ use Shaarli\Front\Controller\Visitor\ErrorController;
12use Shaarli\Front\Controller\Visitor\ErrorNotFoundController; 12use Shaarli\Front\Controller\Visitor\ErrorNotFoundController;
13use Shaarli\History; 13use Shaarli\History;
14use Shaarli\Http\HttpAccess; 14use Shaarli\Http\HttpAccess;
15use Shaarli\Http\MetadataRetriever;
15use Shaarli\Netscape\NetscapeBookmarkUtils; 16use Shaarli\Netscape\NetscapeBookmarkUtils;
16use Shaarli\Plugin\PluginManager; 17use Shaarli\Plugin\PluginManager;
17use Shaarli\Render\PageBuilder; 18use Shaarli\Render\PageBuilder;
@@ -72,6 +73,7 @@ class ContainerBuilderTest extends TestCase
72 static::assertInstanceOf(History::class, $container->history); 73 static::assertInstanceOf(History::class, $container->history);
73 static::assertInstanceOf(HttpAccess::class, $container->httpAccess); 74 static::assertInstanceOf(HttpAccess::class, $container->httpAccess);
74 static::assertInstanceOf(LoginManager::class, $container->loginManager); 75 static::assertInstanceOf(LoginManager::class, $container->loginManager);
76 static::assertInstanceOf(MetadataRetriever::class, $container->metadataRetriever);
75 static::assertInstanceOf(NetscapeBookmarkUtils::class, $container->netscapeBookmarkUtils); 77 static::assertInstanceOf(NetscapeBookmarkUtils::class, $container->netscapeBookmarkUtils);
76 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder); 78 static::assertInstanceOf(PageBuilder::class, $container->pageBuilder);
77 static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager); 79 static::assertInstanceOf(PageCacheManager::class, $container->pageCacheManager);
diff --git a/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php b/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
index 2eb95251..4fd88480 100644
--- a/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
+++ b/tests/front/controller/admin/ManageShaareControllerTest/DisplayCreateFormTest.php
@@ -9,6 +9,7 @@ use Shaarli\Config\ConfigManager;
9use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper; 9use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
10use Shaarli\Front\Controller\Admin\ManageShaareController; 10use Shaarli\Front\Controller\Admin\ManageShaareController;
11use Shaarli\Http\HttpAccess; 11use Shaarli\Http\HttpAccess;
12use Shaarli\Http\MetadataRetriever;
12use Shaarli\TestCase; 13use Shaarli\TestCase;
13use Slim\Http\Request; 14use Slim\Http\Request;
14use Slim\Http\Response; 15use Slim\Http\Response;
@@ -25,6 +26,7 @@ class DisplayCreateFormTest extends TestCase
25 $this->createContainer(); 26 $this->createContainer();
26 27
27 $this->container->httpAccess = $this->createMock(HttpAccess::class); 28 $this->container->httpAccess = $this->createMock(HttpAccess::class);
29 $this->container->metadataRetriever = $this->createMock(MetadataRetriever::class);
28 $this->controller = new ManageShaareController($this->container); 30 $this->controller = new ManageShaareController($this->container);
29 } 31 }
30 32
@@ -32,7 +34,7 @@ class DisplayCreateFormTest extends TestCase
32 * Test displaying bookmark create form 34 * Test displaying bookmark create form
33 * Ensure that every step of the standard workflow works properly. 35 * Ensure that every step of the standard workflow works properly.
34 */ 36 */
35 public function testDisplayCreateFormWithUrl(): void 37 public function testDisplayCreateFormWithUrlAndWithMetadataRetrieval(): void
36 { 38 {
37 $this->container->environment = [ 39 $this->container->environment = [
38 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc' 40 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
@@ -53,40 +55,20 @@ class DisplayCreateFormTest extends TestCase
53 }); 55 });
54 $response = new Response(); 56 $response = new Response();
55 57
56 $this->container->httpAccess 58 $this->container->conf = $this->createMock(ConfigManager::class);
57 ->expects(static::once()) 59 $this->container->conf->method('get')->willReturnCallback(function (string $param, $default) {
58 ->method('getCurlDownloadCallback') 60 if ($param === 'general.enable_async_metadata') {
59 ->willReturnCallback( 61 return false;
60 function (&$charset, &$title, &$description, &$tags) use ( 62 }
61 $remoteTitle, 63
62 $remoteDesc, 64 return $default;
63 $remoteTags 65 });
64 ): callable { 66
65 return function () use ( 67 $this->container->metadataRetriever->expects(static::once())->method('retrieve')->willReturn([
66 &$charset, 68 'title' => $remoteTitle,
67 &$title, 69 'description' => $remoteDesc,
68 &$description, 70 'tags' => $remoteTags,
69 &$tags, 71 ]);
70 $remoteTitle,
71 $remoteDesc,
72 $remoteTags
73 ): void {
74 $charset = 'ISO-8859-1';
75 $title = $remoteTitle;
76 $description = $remoteDesc;
77 $tags = $remoteTags;
78 };
79 }
80 )
81 ;
82 $this->container->httpAccess
83 ->expects(static::once())
84 ->method('getHttpResponse')
85 ->with($expectedUrl, 30, 4194304)
86 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
87 $callback();
88 })
89 ;
90 72
91 $this->container->bookmarkService 73 $this->container->bookmarkService
92 ->expects(static::once()) 74 ->expects(static::once())
@@ -127,6 +109,72 @@ class DisplayCreateFormTest extends TestCase
127 static::assertSame($tags, $assignedVariables['tags']); 109 static::assertSame($tags, $assignedVariables['tags']);
128 static::assertArrayHasKey('source', $assignedVariables); 110 static::assertArrayHasKey('source', $assignedVariables);
129 static::assertArrayHasKey('default_private_links', $assignedVariables); 111 static::assertArrayHasKey('default_private_links', $assignedVariables);
112 static::assertArrayHasKey('async_metadata', $assignedVariables);
113 static::assertArrayHasKey('retrieve_description', $assignedVariables);
114 }
115
116 /**
117 * Test displaying bookmark create form without any external metadata retrieval attempt
118 */
119 public function testDisplayCreateFormWithUrlAndWithoutMetadata(): void
120 {
121 $this->container->environment = [
122 'HTTP_REFERER' => $referer = 'http://shaarli/subfolder/controller/?searchtag=abc'
123 ];
124
125 $assignedVariables = [];
126 $this->assignTemplateVars($assignedVariables);
127
128 $url = 'http://url.tld/other?part=3&utm_ad=pay#hash';
129 $expectedUrl = str_replace('&utm_ad=pay', '', $url);
130
131 $request = $this->createMock(Request::class);
132 $request->method('getParam')->willReturnCallback(function (string $key) use ($url): ?string {
133 return $key === 'post' ? $url : null;
134 });
135 $response = new Response();
136
137 $this->container->metadataRetriever->expects(static::never())->method('retrieve');
138
139 $this->container->bookmarkService
140 ->expects(static::once())
141 ->method('bookmarksCountPerTag')
142 ->willReturn($tags = ['tag1' => 2, 'tag2' => 1])
143 ;
144
145 // Make sure that PluginManager hook is triggered
146 $this->container->pluginManager
147 ->expects(static::at(0))
148 ->method('executeHooks')
149 ->willReturnCallback(function (string $hook, array $data): array {
150 static::assertSame('render_editlink', $hook);
151 static::assertSame('', $data['link']['title']);
152 static::assertSame('', $data['link']['description']);
153
154 return $data;
155 })
156 ;
157
158 $result = $this->controller->displayCreateForm($request, $response);
159
160 static::assertSame(200, $result->getStatusCode());
161 static::assertSame('editlink', (string) $result->getBody());
162
163 static::assertSame('Shaare - Shaarli', $assignedVariables['pagetitle']);
164
165 static::assertSame($expectedUrl, $assignedVariables['link']['url']);
166 static::assertSame('', $assignedVariables['link']['title']);
167 static::assertSame('', $assignedVariables['link']['description']);
168 static::assertSame('', $assignedVariables['link']['tags']);
169 static::assertFalse($assignedVariables['link']['private']);
170
171 static::assertTrue($assignedVariables['link_is_new']);
172 static::assertSame($referer, $assignedVariables['http_referer']);
173 static::assertSame($tags, $assignedVariables['tags']);
174 static::assertArrayHasKey('source', $assignedVariables);
175 static::assertArrayHasKey('default_private_links', $assignedVariables);
176 static::assertArrayHasKey('async_metadata', $assignedVariables);
177 static::assertArrayHasKey('retrieve_description', $assignedVariables);
130 } 178 }
131 179
132 /** 180 /**
diff --git a/tests/http/MetadataRetrieverTest.php b/tests/http/MetadataRetrieverTest.php
new file mode 100644
index 00000000..2a1838e8
--- /dev/null
+++ b/tests/http/MetadataRetrieverTest.php
@@ -0,0 +1,123 @@
1<?php
2
3declare(strict_types=1);
4
5namespace Shaarli\Http;
6
7use PHPUnit\Framework\TestCase;
8use Shaarli\Config\ConfigManager;
9
10class MetadataRetrieverTest extends TestCase
11{
12 /** @var MetadataRetriever */
13 protected $retriever;
14
15 /** @var ConfigManager */
16 protected $conf;
17
18 /** @var HttpAccess */
19 protected $httpAccess;
20
21 public function setUp(): void
22 {
23 $this->conf = $this->createMock(ConfigManager::class);
24 $this->httpAccess = $this->createMock(HttpAccess::class);
25 $this->retriever = new MetadataRetriever($this->conf, $this->httpAccess);
26
27 $this->conf->method('get')->willReturnCallback(function (string $param, $default) {
28 return $default === null ? $param : $default;
29 });
30 }
31
32 /**
33 * Test metadata retrieve() with values returned
34 */
35 public function testFullRetrieval(): void
36 {
37 $url = 'https://domain.tld/link';
38 $remoteTitle = 'Remote Title ';
39 $remoteDesc = 'Sometimes the meta description is relevant.';
40 $remoteTags = 'abc def';
41
42 $expectedResult = [
43 'title' => $remoteTitle,
44 'description' => $remoteDesc,
45 'tags' => $remoteTags,
46 ];
47
48 $this->httpAccess
49 ->expects(static::once())
50 ->method('getCurlDownloadCallback')
51 ->willReturnCallback(
52 function (&$charset, &$title, &$description, &$tags) use (
53 $remoteTitle,
54 $remoteDesc,
55 $remoteTags
56 ): callable {
57 return function () use (
58 &$charset,
59 &$title,
60 &$description,
61 &$tags,
62 $remoteTitle,
63 $remoteDesc,
64 $remoteTags
65 ): void {
66 $charset = 'ISO-8859-1';
67 $title = $remoteTitle;
68 $description = $remoteDesc;
69 $tags = $remoteTags;
70 };
71 }
72 )
73 ;
74 $this->httpAccess
75 ->expects(static::once())
76 ->method('getHttpResponse')
77 ->with($url, 30, 4194304)
78 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
79 $callback();
80 })
81 ;
82
83 $result = $this->retriever->retrieve($url);
84
85 static::assertSame($expectedResult, $result);
86 }
87
88 /**
89 * Test metadata retrieve() without any value
90 */
91 public function testEmptyRetrieval(): void
92 {
93 $url = 'https://domain.tld/link';
94
95 $expectedResult = [
96 'title' => null,
97 'description' => null,
98 'tags' => null,
99 ];
100
101 $this->httpAccess
102 ->expects(static::once())
103 ->method('getCurlDownloadCallback')
104 ->willReturnCallback(
105 function (&$charset, &$title, &$description, &$tags): callable {
106 return function () use (&$charset, &$title, &$description, &$tags): void {};
107 }
108 )
109 ;
110 $this->httpAccess
111 ->expects(static::once())
112 ->method('getHttpResponse')
113 ->with($url, 30, 4194304)
114 ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void {
115 $callback();
116 })
117 ;
118
119 $result = $this->retriever->retrieve($url);
120
121 static::assertSame($expectedResult, $result);
122 }
123}