From 4cf3564d28dc8e4d08a3e64f09ad045ffbde97ae Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Fri, 25 Sep 2020 13:29:36 +0200 Subject: Add a setting to retrieve bookmark metadata asynchrounously - There is a new standalone script (metadata.js) which requests a new controller to get bookmark metadata and fill the form async - This feature is enabled with the new setting: general.enable_async_metadata (enabled by default) - general.retrieve_description is now enabled by default - A small rotating loader animation has a been added to bookmark inputs when metadata is being retrieved (default template) - Custom JS htmlentities has been removed and mathiasbynens/he library is used instead Fixes #1563 --- tests/http/MetadataRetrieverTest.php | 123 +++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 tests/http/MetadataRetrieverTest.php (limited to 'tests/http/MetadataRetrieverTest.php') 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 @@ +conf = $this->createMock(ConfigManager::class); + $this->httpAccess = $this->createMock(HttpAccess::class); + $this->retriever = new MetadataRetriever($this->conf, $this->httpAccess); + + $this->conf->method('get')->willReturnCallback(function (string $param, $default) { + return $default === null ? $param : $default; + }); + } + + /** + * Test metadata retrieve() with values returned + */ + public function testFullRetrieval(): void + { + $url = 'https://domain.tld/link'; + $remoteTitle = 'Remote Title '; + $remoteDesc = 'Sometimes the meta description is relevant.'; + $remoteTags = 'abc def'; + + $expectedResult = [ + 'title' => $remoteTitle, + 'description' => $remoteDesc, + 'tags' => $remoteTags, + ]; + + $this->httpAccess + ->expects(static::once()) + ->method('getCurlDownloadCallback') + ->willReturnCallback( + function (&$charset, &$title, &$description, &$tags) use ( + $remoteTitle, + $remoteDesc, + $remoteTags + ): callable { + return function () use ( + &$charset, + &$title, + &$description, + &$tags, + $remoteTitle, + $remoteDesc, + $remoteTags + ): void { + $charset = 'ISO-8859-1'; + $title = $remoteTitle; + $description = $remoteDesc; + $tags = $remoteTags; + }; + } + ) + ; + $this->httpAccess + ->expects(static::once()) + ->method('getHttpResponse') + ->with($url, 30, 4194304) + ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void { + $callback(); + }) + ; + + $result = $this->retriever->retrieve($url); + + static::assertSame($expectedResult, $result); + } + + /** + * Test metadata retrieve() without any value + */ + public function testEmptyRetrieval(): void + { + $url = 'https://domain.tld/link'; + + $expectedResult = [ + 'title' => null, + 'description' => null, + 'tags' => null, + ]; + + $this->httpAccess + ->expects(static::once()) + ->method('getCurlDownloadCallback') + ->willReturnCallback( + function (&$charset, &$title, &$description, &$tags): callable { + return function () use (&$charset, &$title, &$description, &$tags): void {}; + } + ) + ; + $this->httpAccess + ->expects(static::once()) + ->method('getHttpResponse') + ->with($url, 30, 4194304) + ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void { + $callback(); + }) + ; + + $result = $this->retriever->retrieve($url); + + static::assertSame($expectedResult, $result); + } +} -- cgit v1.2.3 From 5334090be04e66da5cb5c3ad487604b3733c5cac Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 15 Oct 2020 11:20:33 +0200 Subject: Improve metadata retrieval (performances and accuracy) - Use dedicated function to download headers to avoid apply multiple regexps on headers - Also try to extract title from meta tags --- tests/http/MetadataRetrieverTest.php | 45 ++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) (limited to 'tests/http/MetadataRetrieverTest.php') diff --git a/tests/http/MetadataRetrieverTest.php b/tests/http/MetadataRetrieverTest.php index 2a1838e8..3c9eaa0e 100644 --- a/tests/http/MetadataRetrieverTest.php +++ b/tests/http/MetadataRetrieverTest.php @@ -38,6 +38,7 @@ class MetadataRetrieverTest extends TestCase $remoteTitle = 'Remote Title '; $remoteDesc = 'Sometimes the meta description is relevant.'; $remoteTags = 'abc def'; + $remoteCharset = 'utf-8'; $expectedResult = [ 'title' => $remoteTitle, @@ -45,11 +46,28 @@ class MetadataRetrieverTest extends TestCase 'tags' => $remoteTags, ]; + $this->httpAccess + ->expects(static::once()) + ->method('getCurlHeaderCallback') + ->willReturnCallback( + function (&$charset) use ( + $remoteCharset + ): callable { + return function () use ( + &$charset, + $remoteCharset + ): void { + $charset = $remoteCharset; + }; + } + ) + ; $this->httpAccess ->expects(static::once()) ->method('getCurlDownloadCallback') ->willReturnCallback( function (&$charset, &$title, &$description, &$tags) use ( + $remoteCharset, $remoteTitle, $remoteDesc, $remoteTags @@ -59,11 +77,13 @@ class MetadataRetrieverTest extends TestCase &$title, &$description, &$tags, + $remoteCharset, $remoteTitle, $remoteDesc, $remoteTags ): void { - $charset = 'ISO-8859-1'; + static::assertSame($remoteCharset, $charset); + $title = $remoteTitle; $description = $remoteDesc; $tags = $remoteTags; @@ -75,8 +95,9 @@ class MetadataRetrieverTest extends TestCase ->expects(static::once()) ->method('getHttpResponse') ->with($url, 30, 4194304) - ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void { - $callback(); + ->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void { + $headerCallback(); + $dlCallback(); }) ; @@ -102,8 +123,17 @@ class MetadataRetrieverTest extends TestCase ->expects(static::once()) ->method('getCurlDownloadCallback') ->willReturnCallback( - function (&$charset, &$title, &$description, &$tags): callable { - return function () use (&$charset, &$title, &$description, &$tags): void {}; + function (): callable { + return function (): void {}; + } + ) + ; + $this->httpAccess + ->expects(static::once()) + ->method('getCurlHeaderCallback') + ->willReturnCallback( + function (): callable { + return function (): void {}; } ) ; @@ -111,8 +141,9 @@ class MetadataRetrieverTest extends TestCase ->expects(static::once()) ->method('getHttpResponse') ->with($url, 30, 4194304) - ->willReturnCallback(function($url, $timeout, $maxBytes, $callback): void { - $callback(); + ->willReturnCallback(function($url, $timeout, $maxBytes, $headerCallback, $dlCallback): void { + $headerCallback(); + $dlCallback(); }) ; -- cgit v1.2.3