mirror of
https://github.com/wallabag/wallabag.git
synced 2025-09-15 18:57:05 +00:00
Move test files directly under tests/ directory
This commit is contained in:
parent
a37b385c23
commit
24da70e338
117 changed files with 4 additions and 4 deletions
1139
tests/Helper/ContentProxyTest.php
Normal file
1139
tests/Helper/ContentProxyTest.php
Normal file
File diff suppressed because it is too large
Load diff
38
tests/Helper/CryptoProxyTest.php
Normal file
38
tests/Helper/CryptoProxyTest.php
Normal file
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Psr\Log\NullLogger;
|
||||
use Wallabag\CoreBundle\Helper\CryptoProxy;
|
||||
|
||||
class CryptoProxyTest extends TestCase
|
||||
{
|
||||
public function testCrypto()
|
||||
{
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$crypto = new CryptoProxy(sys_get_temp_dir() . '/' . uniqid('', true) . '.txt', $logger);
|
||||
$crypted = $crypto->crypt('test');
|
||||
$decrypted = $crypto->decrypt($crypted);
|
||||
|
||||
$this->assertSame('test', $decrypted);
|
||||
|
||||
$records = $logHandler->getRecords();
|
||||
$this->assertCount(2, $records);
|
||||
$this->assertStringContainsString('Crypto: crypting value', $records[0]['message']);
|
||||
$this->assertStringContainsString('Crypto: decrypting value', $records[1]['message']);
|
||||
}
|
||||
|
||||
public function testDecryptBadValue()
|
||||
{
|
||||
$this->expectException(\RuntimeException::class);
|
||||
$this->expectExceptionMessage('Decrypt fail');
|
||||
|
||||
$crypto = new CryptoProxy(sys_get_temp_dir() . '/' . uniqid('', true) . '.txt', new NullLogger());
|
||||
$crypto->decrypt('badvalue');
|
||||
}
|
||||
}
|
262
tests/Helper/DownloadImagesTest.php
Normal file
262
tests/Helper/DownloadImagesTest.php
Normal file
|
@ -0,0 +1,262 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpClient\MockHttpClient;
|
||||
use Symfony\Component\HttpClient\Response\MockResponse;
|
||||
use Wallabag\CoreBundle\Helper\DownloadImages;
|
||||
|
||||
class DownloadImagesTest extends TestCase
|
||||
{
|
||||
public function dataForSuccessImage()
|
||||
{
|
||||
return [
|
||||
'imgur' => [
|
||||
'<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>',
|
||||
'http://imgur.com/gallery/WxtWY',
|
||||
],
|
||||
'image with &' => [
|
||||
'<div><img src="https://i2.wp.com/www.tvaddons.ag/wp-content/uploads/2017/01/Screen-Shot-2017-01-07-at-10.17.40-PM.jpg?w=640&ssl=1" /></div>',
|
||||
'https://www.tvaddons.ag/realdebrid-kodi-jarvis/',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider dataForSuccessImage
|
||||
*/
|
||||
public function testProcessHtml($html, $url)
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: image/png']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
|
||||
$res = $download->processHtml(123, $html, $url);
|
||||
|
||||
// this the base path of all image (since it's calculated using the entry id: 123)
|
||||
$this->assertStringContainsString('http://wallabag.io/assets/images/9/b/9b0ead26/', $res);
|
||||
}
|
||||
|
||||
public function testProcessHtmlWithBadImage()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse('', ['response_headers' => ['Content-Type: application/json']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processHtml(123, '<div><img src="http://i.imgur.com/T9qgcHc.jpg" /></div>', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertStringContainsString('http://i.imgur.com/T9qgcHc.jpg', $res, 'Image were not replace because of content-type');
|
||||
}
|
||||
|
||||
public function singleImage()
|
||||
{
|
||||
return [
|
||||
['image/pjpeg', 'jpg'],
|
||||
['image/jpeg', 'jpg'],
|
||||
['image/png', 'png'],
|
||||
['image/gif', 'gif'],
|
||||
['image/webp', 'webp'],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider singleImage
|
||||
*/
|
||||
public function testProcessSingleImage($header, $extension)
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: ' . $header]])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertStringContainsString('/assets/images/9/b/9b0ead26/ebe60399.' . $extension, $res);
|
||||
}
|
||||
|
||||
public function testProcessSingleImageWithBadUrl()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse('', ['http_code' => 404])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, 'T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertFalse($res, 'Image can not be found, so it will not be replaced');
|
||||
}
|
||||
|
||||
public function testProcessSingleImageWithBadImage()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse('', ['response_headers' => ['Content-Type: image/png']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, 'http://i.imgur.com/T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertFalse($res, 'Image can not be loaded, so it will not be replaced');
|
||||
}
|
||||
|
||||
public function testProcessSingleImageFailAbsolute()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: image/png']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, '/i.imgur.com/T9qgcHc.jpg', 'imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertFalse($res, 'Absolute image can not be determined, so it will not be replaced');
|
||||
}
|
||||
|
||||
public function testProcessRealImage()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
|
||||
$res = $download->processSingleImage(
|
||||
123,
|
||||
'https://cdn.theconversation.com/files/157200/article/width926/gsj2rjp2-1487348607.jpg',
|
||||
'https://theconversation.com/conversation-avec-gerald-bronner-ce-nest-pas-la-post-verite-qui-nous-menace-mais-lextension-de-notre-credulite-73089'
|
||||
);
|
||||
|
||||
$this->assertStringContainsString('http://wallabag.io/assets/images/9/b/9b0ead26/', $res, 'Content-Type was empty but data is ok for an image');
|
||||
$this->assertStringContainsString('DownloadImages: Checking extension (alternative)', $logHandler->getRecords()[3]['message']);
|
||||
}
|
||||
|
||||
public function testProcessImageWithSrcset()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processHtml(123, '<p><img class="alignnone wp-image-1153" src="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg" alt="" width="628" height="444" srcset="http://piketty.blog.lemonde.fr/files/2017/10/F1FR-530x375.jpg 530w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-768x543.jpg 768w, http://piketty.blog.lemonde.fr/files/2017/10/F1FR-900x636.jpg 900w" sizes="(max-width: 628px) 100vw, 628px" /></p>', 'http://piketty.blog.lemonde.fr/2017/10/12/budget-2018-la-jeunesse-sacrifiee/');
|
||||
|
||||
$this->assertStringNotContainsString('http://piketty.blog.lemonde.fr/', $res, 'Image srcset attribute were not replaced');
|
||||
}
|
||||
|
||||
public function testProcessImageWithTrickySrcset()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg')),
|
||||
]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processHtml(123, '<figure id="post-257260" class="align-none media-257260"><img src="https://cdn.css-tricks.com/wp-content/uploads/2017/08/the-critical-request.png" srcset="https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_1000,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 1000w, https://res.cloudinary.com/css-tricks/image/upload/c_scale,w_200,f_auto,q_auto/v1501594717/the-critical-request_bqdfaa.png 200w" sizes="(min-width: 1850px) calc( (100vw - 555px) / 3 )
|
||||
(min-width: 1251px) calc( (100vw - 530px) / 2 )
|
||||
(min-width: 1086px) calc(100vw - 480px)
|
||||
(min-width: 626px) calc(100vw - 335px)
|
||||
calc(100vw - 30px)" alt="" /></figure>', 'https://css-tricks.com/the-critical-request/');
|
||||
|
||||
$this->assertStringNotContainsString('f_auto,q_auto', $res, 'Image srcset attribute were not replaced');
|
||||
}
|
||||
|
||||
public function testProcessImageWithNumericHtmlEntitySeparator()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'), ['response_headers' => ['Content-Type: image/jpeg']]),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'), ['response_headers' => ['Content-Type: image/jpeg']]),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'), ['response_headers' => ['Content-Type: image/jpeg']]),
|
||||
]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
// wordpress.com sites using & as an & alternative
|
||||
$res = $download->processHtml(123, '<img srcset="https://example.com/20191204_133626-scaled.jpg?strip=info&w=600&ssl=1 600w,https://example.com/20191204_133626-scaled.jpg?strip=info&w=900&ssl=1 900w" src="https://example.com/20191204_133626-scaled.jpg?ssl=1"/>', 'https://example.com/about/');
|
||||
|
||||
$this->assertStringNotContainsString('https://example.com', $res, 'Image srcset attribute were not replaced');
|
||||
}
|
||||
|
||||
public function testProcessImageWithNullPath()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/image-no-content-type.jpg'))]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
|
||||
$res = $download->processSingleImage(
|
||||
123,
|
||||
null,
|
||||
'https://framablog.org/2018/06/30/engagement-atypique/'
|
||||
);
|
||||
$this->assertFalse($res);
|
||||
}
|
||||
|
||||
public function testEnsureOnlyFirstOccurrenceIsReplaced()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: image/png']]),
|
||||
new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: image/png']]),
|
||||
]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
|
||||
$html = '<img src="https://images.wsj.net/im-410981?width=860&height=573" srcset="https://images.wsj.net/im-410981?width=860&height=573&pixel_ratio=1.5 1290w" height="573" width="860" alt="" referrerpolicy="no-referrer">';
|
||||
$url = 'https://www.wsj.com/articles/5-interior-design-tips-to-max-out-your-basement-space-11633435201';
|
||||
|
||||
$res = $download->processHtml(123, $html, $url);
|
||||
|
||||
$this->assertSame('<img src="http://wallabag.io/assets/images/9/b/9b0ead26/6bef06fe.png" srcset="http://wallabag.io/assets/images/9/b/9b0ead26/43cc0123.png 1290w" height="573" width="860" alt="" referrerpolicy="no-referrer">', $res);
|
||||
}
|
||||
|
||||
public function testProcessSingleImageWithSvg()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/modal-content.svg'), ['response_headers' => ['Content-Type: image/svg+xml']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, 'modal-content.svg', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertStringContainsString('/assets/images/9/b/9b0ead26/400e29f9.svg', $res);
|
||||
}
|
||||
|
||||
public function testProcessSingleImageWithBadSvg()
|
||||
{
|
||||
$mockHttpClient = new MockHttpClient([new MockResponse(file_get_contents(__DIR__ . '/../fixtures/unnamed.png'), ['response_headers' => ['Content-Type: image/svg+xml']])]);
|
||||
|
||||
$logHandler = new TestHandler();
|
||||
$logger = new Logger('test', [$logHandler]);
|
||||
|
||||
$download = new DownloadImages($mockHttpClient, sys_get_temp_dir() . '/wallabag_test', 'http://wallabag.io/', $logger);
|
||||
$res = $download->processSingleImage(123, 'modal-content.svg', 'http://imgur.com/gallery/WxtWY');
|
||||
|
||||
$this->assertFalse($res);
|
||||
}
|
||||
}
|
118
tests/Helper/RedirectTest.php
Normal file
118
tests/Helper/RedirectTest.php
Normal file
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Routing\Router;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
|
||||
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
use Wallabag\CoreBundle\Entity\User;
|
||||
use Wallabag\CoreBundle\Helper\Redirect;
|
||||
|
||||
class RedirectTest extends TestCase
|
||||
{
|
||||
/** @var Router&MockObject */
|
||||
private $routerMock;
|
||||
|
||||
/** @var Redirect */
|
||||
private $redirect;
|
||||
|
||||
/** @var User */
|
||||
private $user;
|
||||
|
||||
/** @var UsernamePasswordToken */
|
||||
private $token;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->routerMock = $this->getMockBuilder(Router::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$this->routerMock->expects($this->any())
|
||||
->method('generate')
|
||||
->with('homepage')
|
||||
->willReturn('/');
|
||||
|
||||
$this->user = new User();
|
||||
$this->user->setName('youpi');
|
||||
$this->user->setEmail('youpi@youpi.org');
|
||||
$this->user->setUsername('youpi');
|
||||
$this->user->setPlainPassword('youpi');
|
||||
$this->user->setEnabled(true);
|
||||
$this->user->addRole('ROLE_SUPER_ADMIN');
|
||||
|
||||
$config = new Config($this->user);
|
||||
$config->setItemsPerPage(30);
|
||||
$config->setReadingSpeed(200);
|
||||
$config->setLanguage('en');
|
||||
$config->setPocketConsumerKey('xxxxx');
|
||||
$config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
|
||||
|
||||
$this->user->setConfig($config);
|
||||
|
||||
$this->token = new UsernamePasswordToken($this->user, 'password', 'key');
|
||||
$tokenStorage = new TokenStorage();
|
||||
$tokenStorage->setToken($this->token);
|
||||
|
||||
$this->redirect = new Redirect($this->routerMock, $tokenStorage);
|
||||
}
|
||||
|
||||
public function testRedirectToNull()
|
||||
{
|
||||
$redirectUrl = $this->redirect->to(null);
|
||||
|
||||
$this->assertSame('/', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testRedirectToValidUrl()
|
||||
{
|
||||
$redirectUrl = $this->redirect->to('/unread/list');
|
||||
|
||||
$this->assertSame('/unread/list', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testRedirectToAbsoluteUrl()
|
||||
{
|
||||
$redirectUrl = $this->redirect->to('https://www.google.com/');
|
||||
|
||||
$this->assertSame('/', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testWithNotLoggedUser()
|
||||
{
|
||||
$redirect = new Redirect($this->routerMock, new TokenStorage());
|
||||
$redirectUrl = $redirect->to('/unread/list');
|
||||
|
||||
$this->assertSame('/unread/list', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testUserForRedirectToHomepage()
|
||||
{
|
||||
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
|
||||
|
||||
$redirectUrl = $this->redirect->to('/unread/list');
|
||||
|
||||
$this->assertSame('/', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testUserForRedirectWithIgnoreActionMarkAsRead()
|
||||
{
|
||||
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
|
||||
|
||||
$redirectUrl = $this->redirect->to('/unread/list', true);
|
||||
|
||||
$this->assertSame('/unread/list', $redirectUrl);
|
||||
}
|
||||
|
||||
public function testUserForRedirectNullWithIgnoreActionMarkAsRead()
|
||||
{
|
||||
$this->user->getConfig()->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
|
||||
|
||||
$redirectUrl = $this->redirect->to(null, true);
|
||||
|
||||
$this->assertSame('/', $redirectUrl);
|
||||
}
|
||||
}
|
214
tests/Helper/RuleBasedIgnoreOriginProcessorTest.php
Normal file
214
tests/Helper/RuleBasedIgnoreOriginProcessorTest.php
Normal file
|
@ -0,0 +1,214 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RulerZ\RulerZ;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\IgnoreOriginInstanceRule;
|
||||
use Wallabag\CoreBundle\Entity\IgnoreOriginUserRule;
|
||||
use Wallabag\CoreBundle\Entity\User;
|
||||
use Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor;
|
||||
use Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository;
|
||||
|
||||
class RuleBasedIgnoreOriginProcessorTest extends TestCase
|
||||
{
|
||||
private $rulerz;
|
||||
private $processor;
|
||||
private $ignoreOriginInstanceRuleRepository;
|
||||
private $logger;
|
||||
private $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->rulerz = $this->getRulerZMock();
|
||||
$this->logger = $this->getLogger();
|
||||
$this->ignoreOriginInstanceRuleRepository = $this->getIgnoreOriginInstanceRuleRepositoryMock();
|
||||
$this->handler = new TestHandler();
|
||||
$this->logger->pushHandler($this->handler);
|
||||
|
||||
$this->processor = new RuleBasedIgnoreOriginProcessor($this->rulerz, $this->logger, $this->ignoreOriginInstanceRuleRepository);
|
||||
}
|
||||
|
||||
public function testProcessWithNoRule()
|
||||
{
|
||||
$user = $this->getUser();
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([]);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->never())
|
||||
->method('satisfies');
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testProcessWithNoMatchingRule()
|
||||
{
|
||||
$userRule = $this->getIgnoreOriginUserRule('rule as string');
|
||||
$user = $this->getUser([$userRule]);
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([]);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->willReturn(false);
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
public function testProcessWithAMatchingRule()
|
||||
{
|
||||
$userRule = $this->getIgnoreOriginUserRule('rule as string');
|
||||
$user = $this->getUser([$userRule]);
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([]);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->willReturn(true);
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testProcessWithAMixOfMatchingRules()
|
||||
{
|
||||
$userRule = $this->getIgnoreOriginUserRule('rule as string');
|
||||
$anotherUserRule = $this->getIgnoreOriginUserRule('another rule as string');
|
||||
$user = $this->getUser([$userRule, $anotherUserRule]);
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([]);
|
||||
|
||||
$this->rulerz
|
||||
->method('satisfies')
|
||||
->will($this->onConsecutiveCalls(false, true));
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testProcessWithInstanceRules()
|
||||
{
|
||||
$user = $this->getUser();
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([$instanceRule]);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->willReturn(true);
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testProcessWithMixedRules()
|
||||
{
|
||||
$userRule = $this->getIgnoreOriginUserRule('rule as string');
|
||||
$user = $this->getUser([$userRule]);
|
||||
$entry = new Entry($user);
|
||||
$entry->setUrl('http://example.com/hello-world');
|
||||
|
||||
$instanceRule = $this->getIgnoreOriginInstanceRule('rule as string');
|
||||
$this->ignoreOriginInstanceRuleRepository
|
||||
->expects($this->once())
|
||||
->method('findAll')
|
||||
->willReturn([$instanceRule]);
|
||||
|
||||
$this->rulerz
|
||||
->method('satisfies')
|
||||
->will($this->onConsecutiveCalls(false, true));
|
||||
|
||||
$result = $this->processor->process($entry);
|
||||
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
private function getUser(array $ignoreOriginRules = [])
|
||||
{
|
||||
$user = new User();
|
||||
$config = new Config($user);
|
||||
|
||||
$user->setConfig($config);
|
||||
|
||||
foreach ($ignoreOriginRules as $rule) {
|
||||
$config->addIgnoreOriginRule($rule);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function getIgnoreOriginUserRule($rule)
|
||||
{
|
||||
$ignoreOriginUserRule = new IgnoreOriginUserRule();
|
||||
$ignoreOriginUserRule->setRule($rule);
|
||||
|
||||
return $ignoreOriginUserRule;
|
||||
}
|
||||
|
||||
private function getIgnoreOriginInstanceRule($rule)
|
||||
{
|
||||
$ignoreOriginInstanceRule = new IgnoreOriginInstanceRule();
|
||||
$ignoreOriginInstanceRule->setRule($rule);
|
||||
|
||||
return $ignoreOriginInstanceRule;
|
||||
}
|
||||
|
||||
private function getRulerZMock()
|
||||
{
|
||||
return $this->getMockBuilder(RulerZ::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
private function getIgnoreOriginInstanceRuleRepositoryMock()
|
||||
{
|
||||
return $this->getMockBuilder(IgnoreOriginInstanceRuleRepository::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
private function getLogger()
|
||||
{
|
||||
return new Logger('foo');
|
||||
}
|
||||
}
|
294
tests/Helper/RuleBasedTaggerTest.php
Normal file
294
tests/Helper/RuleBasedTaggerTest.php
Normal file
|
@ -0,0 +1,294 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use Doctrine\ORM\AbstractQuery;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Monolog\Handler\TestHandler;
|
||||
use Monolog\Logger;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RulerZ\RulerZ;
|
||||
use Wallabag\CoreBundle\Entity\Config;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\Tag;
|
||||
use Wallabag\CoreBundle\Entity\TaggingRule;
|
||||
use Wallabag\CoreBundle\Entity\User;
|
||||
use Wallabag\CoreBundle\Helper\RuleBasedTagger;
|
||||
use Wallabag\CoreBundle\Repository\EntryRepository;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
|
||||
class RuleBasedTaggerTest extends TestCase
|
||||
{
|
||||
private $rulerz;
|
||||
private $tagRepository;
|
||||
private $entryRepository;
|
||||
private $tagger;
|
||||
private $logger;
|
||||
private $handler;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->rulerz = $this->getRulerZMock();
|
||||
$this->tagRepository = $this->getTagRepositoryMock();
|
||||
$this->entryRepository = $this->getEntryRepositoryMock();
|
||||
$this->logger = $this->getLogger();
|
||||
$this->handler = new TestHandler();
|
||||
$this->logger->pushHandler($this->handler);
|
||||
|
||||
$this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository, $this->logger);
|
||||
}
|
||||
|
||||
public function testTagWithNoRule()
|
||||
{
|
||||
$entry = new Entry($this->getUser());
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertTrue($entry->getTags()->isEmpty());
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(0, $records);
|
||||
}
|
||||
|
||||
public function testTagWithNoMatchingRule()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
|
||||
$user = $this->getUser([$taggingRule]);
|
||||
$entry = new Entry($user);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->with($entry, 'rule as string')
|
||||
->willReturn(false);
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertTrue($entry->getTags()->isEmpty());
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(0, $records);
|
||||
}
|
||||
|
||||
public function testTagWithAMatchingRule()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('rule as string', ['foo', 'bar']);
|
||||
$user = $this->getUser([$taggingRule]);
|
||||
$entry = new Entry($user);
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->with($entry, 'rule as string')
|
||||
->willReturn(true);
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertFalse($entry->getTags()->isEmpty());
|
||||
|
||||
$tags = $entry->getTags();
|
||||
$this->assertSame('foo', $tags[0]->getLabel());
|
||||
$this->assertSame('bar', $tags[1]->getLabel());
|
||||
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(1, $records);
|
||||
}
|
||||
|
||||
public function testTagWithAMixOfMatchingRules()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
||||
$otherTaggingRule = $this->getTaggingRule('rule as string', ['foo']);
|
||||
|
||||
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
|
||||
$entry = new Entry($user);
|
||||
|
||||
$this->rulerz
|
||||
->method('satisfies')
|
||||
->will($this->onConsecutiveCalls(false, true));
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertFalse($entry->getTags()->isEmpty());
|
||||
|
||||
$tags = $entry->getTags();
|
||||
$this->assertSame('foo', $tags[0]->getLabel());
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(1, $records);
|
||||
}
|
||||
|
||||
public function testWhenTheTagExists()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('rule as string', ['foo']);
|
||||
$user = $this->getUser([$taggingRule]);
|
||||
$entry = new Entry($user);
|
||||
$tag = new Tag();
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->with($entry, 'rule as string')
|
||||
->willReturn(true);
|
||||
|
||||
$this->tagRepository
|
||||
->expects($this->once())
|
||||
// the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
|
||||
// to magically call the `findOneBy` with ['label' => 'foo']
|
||||
->method('__call')
|
||||
->willReturn($tag);
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertFalse($entry->getTags()->isEmpty());
|
||||
|
||||
$tags = $entry->getTags();
|
||||
$this->assertSame($tag, $tags[0]);
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(1, $records);
|
||||
}
|
||||
|
||||
public function testWithMixedCaseTag()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('rule as string', ['Foo']);
|
||||
$user = $this->getUser([$taggingRule]);
|
||||
$entry = new Entry($user);
|
||||
$tag = new Tag();
|
||||
|
||||
$this->rulerz
|
||||
->expects($this->once())
|
||||
->method('satisfies')
|
||||
->with($entry, 'rule as string')
|
||||
->willReturn(true);
|
||||
|
||||
$this->tagRepository
|
||||
->expects($this->once())
|
||||
// the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
|
||||
// to magically call the `findOneBy` with ['label' => 'foo']
|
||||
->method('__call')
|
||||
->with('findOneByLabel', ['foo'])
|
||||
->willReturn($tag);
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertFalse($entry->getTags()->isEmpty());
|
||||
|
||||
$tags = $entry->getTags();
|
||||
$this->assertSame($tag, $tags[0]);
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(1, $records);
|
||||
}
|
||||
|
||||
public function testSameTagWithDifferentfMatchingRules()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
||||
$otherTaggingRule = $this->getTaggingRule('rule as string', ['hey']);
|
||||
|
||||
$user = $this->getUser([$taggingRule, $otherTaggingRule]);
|
||||
$entry = new Entry($user);
|
||||
|
||||
$this->rulerz
|
||||
->method('satisfies')
|
||||
->willReturn(true);
|
||||
|
||||
$this->tagger->tag($entry);
|
||||
|
||||
$this->assertFalse($entry->getTags()->isEmpty());
|
||||
|
||||
$tags = $entry->getTags();
|
||||
$this->assertCount(1, $tags);
|
||||
$records = $this->handler->getRecords();
|
||||
$this->assertCount(2, $records);
|
||||
}
|
||||
|
||||
public function testTagAllEntriesForAUser()
|
||||
{
|
||||
$taggingRule = $this->getTaggingRule('bla bla', ['hey']);
|
||||
|
||||
$user = $this->getUser([$taggingRule]);
|
||||
|
||||
$this->rulerz
|
||||
->method('satisfies')
|
||||
->willReturn(true);
|
||||
|
||||
$query = $this->getMockBuilder(AbstractQuery::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$query
|
||||
->expects($this->once())
|
||||
->method('getResult')
|
||||
->willReturn([new Entry($user), new Entry($user)]);
|
||||
|
||||
$qb = $this->getMockBuilder(QueryBuilder::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
|
||||
$qb
|
||||
->expects($this->once())
|
||||
->method('getQuery')
|
||||
->willReturn($query);
|
||||
|
||||
$this->entryRepository
|
||||
->expects($this->once())
|
||||
->method('getBuilderForAllByUser')
|
||||
->willReturn($qb);
|
||||
|
||||
$entries = $this->tagger->tagAllForUser($user);
|
||||
|
||||
$this->assertCount(2, $entries);
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
$tags = $entry->getTags();
|
||||
|
||||
$this->assertCount(1, $tags);
|
||||
$this->assertSame('hey', $tags[0]->getLabel());
|
||||
}
|
||||
}
|
||||
|
||||
private function getUser(array $taggingRules = [])
|
||||
{
|
||||
$user = new User();
|
||||
$config = new Config($user);
|
||||
$config->setReadingSpeed(200);
|
||||
|
||||
$user->setConfig($config);
|
||||
|
||||
foreach ($taggingRules as $rule) {
|
||||
$config->addTaggingRule($rule);
|
||||
}
|
||||
|
||||
return $user;
|
||||
}
|
||||
|
||||
private function getTaggingRule($rule, array $tags)
|
||||
{
|
||||
$taggingRule = new TaggingRule();
|
||||
$taggingRule->setRule($rule);
|
||||
$taggingRule->setTags($tags);
|
||||
|
||||
return $taggingRule;
|
||||
}
|
||||
|
||||
private function getRulerZMock()
|
||||
{
|
||||
return $this->getMockBuilder(RulerZ::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
private function getTagRepositoryMock()
|
||||
{
|
||||
return $this->getMockBuilder(TagRepository::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
private function getEntryRepositoryMock()
|
||||
{
|
||||
return $this->getMockBuilder(EntryRepository::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
|
||||
private function getLogger()
|
||||
{
|
||||
return new Logger('foo');
|
||||
}
|
||||
}
|
109
tests/Helper/TagsAssignerTest.php
Normal file
109
tests/Helper/TagsAssignerTest.php
Normal file
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Wallabag\CoreBundle\Helper;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Wallabag\CoreBundle\Entity\Entry;
|
||||
use Wallabag\CoreBundle\Entity\Tag;
|
||||
use Wallabag\CoreBundle\Entity\User;
|
||||
use Wallabag\CoreBundle\Helper\TagsAssigner;
|
||||
use Wallabag\CoreBundle\Repository\TagRepository;
|
||||
|
||||
class TagsAssignerTest extends TestCase
|
||||
{
|
||||
public function testAssignTagsWithArrayAndExtraSpaces()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$entry = new Entry(new User());
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, [' tag1', 'tag2 ']);
|
||||
|
||||
$this->assertCount(2, $entry->getTags());
|
||||
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
|
||||
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
|
||||
}
|
||||
|
||||
public function testAssignTagsWithString()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$entry = new Entry(new User());
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
|
||||
|
||||
$this->assertCount(2, $entry->getTags());
|
||||
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
|
||||
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
|
||||
}
|
||||
|
||||
public function testAssignTagsWithEmptyArray()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$entry = new Entry(new User());
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, []);
|
||||
|
||||
$this->assertCount(0, $entry->getTags());
|
||||
}
|
||||
|
||||
public function testAssignTagsWithEmptyString()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$entry = new Entry(new User());
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, '');
|
||||
|
||||
$this->assertCount(0, $entry->getTags());
|
||||
}
|
||||
|
||||
public function testAssignTagsAlreadyAssigned()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$tagEntity = new Tag();
|
||||
$tagEntity->setLabel('tag1');
|
||||
|
||||
$entry = new Entry(new User());
|
||||
$entry->addTag($tagEntity);
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, 'tag1, tag2');
|
||||
|
||||
$this->assertCount(2, $entry->getTags());
|
||||
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
|
||||
$this->assertSame('tag2', $entry->getTags()[1]->getLabel());
|
||||
}
|
||||
|
||||
public function testAssignTagsNotFlushed()
|
||||
{
|
||||
$tagRepo = $this->getTagRepositoryMock();
|
||||
$tagRepo->expects($this->never())
|
||||
->method('__call');
|
||||
|
||||
$tagsAssigner = new TagsAssigner($tagRepo);
|
||||
|
||||
$tagEntity = new Tag();
|
||||
$tagEntity->setLabel('tag1');
|
||||
|
||||
$entry = new Entry(new User());
|
||||
|
||||
$tagsAssigner->assignTagsToEntry($entry, 'tag1', [$tagEntity]);
|
||||
|
||||
$this->assertCount(1, $entry->getTags());
|
||||
$this->assertSame('tag1', $entry->getTags()[0]->getLabel());
|
||||
}
|
||||
|
||||
private function getTagRepositoryMock()
|
||||
{
|
||||
return $this->getMockBuilder(TagRepository::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue