From 24230a5130005e274e1d8d3fe8eaca13cb978b9c Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 23 Jun 2019 22:13:44 +0200 Subject: Add new Ignore Origin rules tab, update ConfigController Signed-off-by: Kevin Decherf --- .../CoreBundle/Controller/ConfigControllerTest.php | 148 ++++++++++++++++++++- 1 file changed, 142 insertions(+), 6 deletions(-) (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php index b3b3a19a..40a1aa97 100644 --- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php @@ -435,7 +435,6 @@ class ConfigControllerTest extends WallabagCoreTestCase public function testTaggingRuleCreation() { $this->logInAs('admin'); - $this->useTheme('baggy'); $client = $this->getClient(); $crawler = $client->request('GET', '/config'); @@ -457,7 +456,7 @@ class ConfigControllerTest extends WallabagCoreTestCase $this->assertContains('flashes.config.notice.tagging_rules_updated', $crawler->filter('body')->extract(['_text'])[0]); - $editLink = $crawler->filter('.mode_edit')->last()->link(); + $editLink = $crawler->filter('div[id=set5] a.mode_edit')->last()->link(); $crawler = $client->click($editLink); $this->assertSame(302, $client->getResponse()->getStatusCode()); @@ -482,7 +481,7 @@ class ConfigControllerTest extends WallabagCoreTestCase $this->assertContains('readingTime <= 30', $crawler->filter('body')->extract(['_text'])[0]); - $deleteLink = $crawler->filter('.delete')->last()->link(); + $deleteLink = $crawler->filter('div[id=set5] a.delete')->last()->link(); $crawler = $client->click($deleteLink); $this->assertSame(302, $client->getResponse()->getStatusCode()); @@ -574,11 +573,11 @@ class ConfigControllerTest extends WallabagCoreTestCase ->getRepository('WallabagCoreBundle:TaggingRule') ->findAll()[0]; - $crawler = $client->request('GET', '/tagging-rule/edit/' . $rule->getId()); + $crawler = $client->request('GET', '/tagging-rule/delete/' . $rule->getId()); $this->assertSame(403, $client->getResponse()->getStatusCode()); $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); - $this->assertContains('You can not access this tagging rule', $body[0]); + $this->assertContains('You can not access this rule', $body[0]); } public function testEditingTaggingRuleFromAnOtherUser() @@ -594,7 +593,144 @@ class ConfigControllerTest extends WallabagCoreTestCase $this->assertSame(403, $client->getResponse()->getStatusCode()); $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); - $this->assertContains('You can not access this tagging rule', $body[0]); + $this->assertContains('You can not access this rule', $body[0]); + } + + public function testIgnoreOriginRuleCreation() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form(); + + $data = [ + 'ignore_origin_user_rule[rule]' => 'host = "example.com"', + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.config.notice.ignore_origin_rules_updated', $crawler->filter('body')->extract(['_text'])[0]); + + $editLink = $crawler->filter('div[id=set6] a.mode_edit')->last()->link(); + + $crawler = $client->click($editLink); + $this->assertSame(302, $client->getResponse()->getStatusCode()); + $this->assertContains('?ignore-origin-user-rule=', $client->getResponse()->headers->get('location')); + + $crawler = $client->followRedirect(); + + $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form(); + + $data = [ + 'ignore_origin_user_rule[rule]' => 'host = "example.org"', + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.config.notice.ignore_origin_rules_updated', $crawler->filter('body')->extract(['_text'])[0]); + + $this->assertContains('host = "example.org"', $crawler->filter('body')->extract(['_text'])[0]); + + $deleteLink = $crawler->filter('div[id=set6] a.delete')->last()->link(); + + $crawler = $client->click($deleteLink); + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + $this->assertContains('flashes.config.notice.ignore_origin_rules_deleted', $crawler->filter('body')->extract(['_text'])[0]); + } + + public function dataForIgnoreOriginRuleCreationFail() + { + return [ + [ + [ + 'ignore_origin_user_rule[rule]' => 'foo = "bar"', + ], + [ + 'The variable', + 'does not exist.', + ], + ], + [ + [ + 'ignore_origin_user_rule[rule]' => '_all != "none"', + ], + [ + 'The operator', + 'does not exist.', + ], + ], + ]; + } + + /** + * @dataProvider dataForIgnoreOriginRuleCreationFail + */ + public function testIgnoreOriginRuleCreationFail($data, $messages) + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[id=ignore_origin_user_rule_save]')->form(); + + $crawler = $client->submit($form, $data); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + + foreach ($messages as $message) { + $this->assertContains($message, $body[0]); + } + } + + public function testDeletingIgnoreOriginRuleFromAnOtherUser() + { + $this->logInAs('bob'); + $client = $this->getClient(); + + $rule = $client->getContainer()->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:IgnoreOriginUserRule') + ->findAll()[0]; + + $crawler = $client->request('GET', '/ignore-origin-user-rule/edit/' . $rule->getId()); + + $this->assertSame(403, $client->getResponse()->getStatusCode()); + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + $this->assertContains('You can not access this rule', $body[0]); + } + + public function testEditingIgnoreOriginRuleFromAnOtherUser() + { + $this->logInAs('bob'); + $client = $this->getClient(); + + $rule = $client->getContainer()->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:IgnoreOriginUserRule') + ->findAll()[0]; + + $crawler = $client->request('GET', '/ignore-origin-user-rule/edit/' . $rule->getId()); + + $this->assertSame(403, $client->getResponse()->getStatusCode()); + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + $this->assertContains('You can not access this rule', $body[0]); } public function testDemoMode() -- cgit v1.2.3 From f39c5a2a702036750b4d7c32d02e7f92955a4eed Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 11 Aug 2019 23:51:55 +0200 Subject: Add new Helper to process Ignore Origin rules and RulerZ operator This commits adds a new helper like RuleBasedTagger for processing ignore origin rules. It also adds a new custom RulerZ operator for the '~' pattern matching rule. Renames 'pattern' with '_all' in IgnoreOriginRule entity. Signed-off-by: Kevin Decherf --- .../Helper/RuleBasedIgnoreOriginProcessorTest.php | 212 +++++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php b/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php new file mode 100644 index 00000000..9e39bc81 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessorTest.php @@ -0,0 +1,212 @@ +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\RulerZ') + ->disableOriginalConstructor() + ->getMock(); + } + + private function getIgnoreOriginInstanceRuleRepositoryMock() + { + return $this->getMockBuilder('Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository') + ->disableOriginalConstructor() + ->getMock(); + } + + private function getLogger() + { + return new Logger('foo'); + } +} -- cgit v1.2.3 From b22eb276232b5c15a6fbadc9dd10144e709faec3 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sun, 11 Aug 2019 23:55:52 +0200 Subject: ContentProxy: replace ignoreUrl with new RuleBasedIgnoreOriginProcessor Signed-off-by: Kevin Decherf --- .../CoreBundle/Helper/ContentProxyTest.php | 127 +++++++++++++++++---- 1 file changed, 104 insertions(+), 23 deletions(-) (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php index 9ce72c79..a65ac17c 100644 --- a/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php +++ b/tests/Wallabag/CoreBundle/Helper/ContentProxyTest.php @@ -12,6 +12,7 @@ use Symfony\Component\Validator\ConstraintViolationList; use Symfony\Component\Validator\Validator\RecursiveValidator; use Wallabag\CoreBundle\Entity\Entry; use Wallabag\CoreBundle\Helper\ContentProxy; +use Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor; use Wallabag\CoreBundle\Helper\RuleBasedTagger; use Wallabag\UserBundle\Entity\User; @@ -25,6 +26,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -42,7 +45,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://user@:80'); @@ -62,6 +65,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -79,7 +84,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -99,6 +104,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -118,7 +125,7 @@ class ContentProxyTest extends TestCase 'description' => 'desc', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://domain.io'); @@ -139,6 +146,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -159,7 +170,7 @@ class ContentProxyTest extends TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -180,6 +191,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -200,7 +215,7 @@ class ContentProxyTest extends TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -221,6 +236,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -240,7 +259,7 @@ class ContentProxyTest extends TestCase 'image' => null, ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -261,6 +280,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -280,7 +303,7 @@ class ContentProxyTest extends TestCase 'image' => 'http://3.3.3.3/cover.jpg', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -301,6 +324,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $validator = $this->getValidator(false); $validator->expects($this->once()) ->method('validate') @@ -324,7 +351,7 @@ class ContentProxyTest extends TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $validator, $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -344,6 +371,10 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + $validator = $this->getValidator(false); $validator->expects($this->exactly(2)) ->method('validate') @@ -372,7 +403,7 @@ class ContentProxyTest extends TestCase 'image' => 'https://', ]); - $proxy = new ContentProxy($graby, $tagger, $validator, $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $validator, $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -393,7 +424,11 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process'); + + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true); $entry = new Entry(new User()); $proxy->updateEntry( $entry, @@ -433,10 +468,12 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $logHandler = new TestHandler(); $logger = new Logger('test', [$logHandler]); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $logger, $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry( $entry, @@ -469,11 +506,13 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $logger = new Logger('foo'); $handler = new TestHandler(); $logger->pushHandler($handler); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $logger, $this->fetchingErrorMessage); + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $logger, $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry( $entry, @@ -512,7 +551,9 @@ class ContentProxyTest extends TestCase ->method('tag') ->will($this->throwException(new \Exception())); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry( $entry, @@ -554,7 +595,9 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry( $entry, @@ -590,6 +633,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -607,7 +652,7 @@ class ContentProxyTest extends TestCase ], ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -631,6 +676,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -648,7 +695,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -668,6 +715,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -685,7 +734,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -704,6 +753,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -721,7 +772,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -740,6 +791,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -757,7 +810,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -776,6 +829,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -793,7 +848,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -813,6 +868,8 @@ class ContentProxyTest extends TestCase $tagger->expects($this->once()) ->method('tag'); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $graby = $this->getMockBuilder('Graby\Graby') ->setMethods(['fetchContent']) ->disableOriginalConstructor() @@ -830,7 +887,7 @@ class ContentProxyTest extends TestCase 'language' => '', ]); - $proxy = new ContentProxy($graby, $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); + $proxy = new ContentProxy($graby, $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage); $entry = new Entry(new User()); $proxy->updateEntry($entry, 'http://0.0.0.0'); @@ -850,6 +907,7 @@ class ContentProxyTest extends TestCase * $expected_entry_url * $expected_origin_url * $expected_domain + * $processor_result */ public function dataForChangedUrl() { @@ -861,6 +919,7 @@ class ContentProxyTest extends TestCase 'http://1.1.1.1', 'http://0.0.0.0', '1.1.1.1', + false, ], 'origin already set' => [ 'http://0.0.0.0', @@ -869,6 +928,7 @@ class ContentProxyTest extends TestCase 'http://1.1.1.1', 'http://hello', '1.1.1.1', + false, ], 'trailing slash' => [ 'https://example.com/hello-world', @@ -877,6 +937,7 @@ class ContentProxyTest extends TestCase 'https://example.com/hello-world/', null, 'example.com', + false, ], 'query string in fetched content' => [ 'https://example.org/hello', @@ -885,6 +946,7 @@ class ContentProxyTest extends TestCase 'https://example.org/hello?world=1', 'https://example.org/hello', 'example.org', + false, ], 'fragment in fetched content' => [ 'https://example.org/hello', @@ -893,6 +955,7 @@ class ContentProxyTest extends TestCase 'https://example.org/hello', null, 'example.org', + false, ], 'fragment and query string in fetched content' => [ 'https://example.org/hello', @@ -901,6 +964,7 @@ class ContentProxyTest extends TestCase 'https://example.org/hello?foo#world', 'https://example.org/hello', 'example.org', + false, ], 'different path and query string in fetch content' => [ 'https://example.org/hello', @@ -909,6 +973,7 @@ class ContentProxyTest extends TestCase 'https://example.org/world?foo', 'https://example.org/hello', 'example.org', + false, ], 'feedproxy ignore list test' => [ 'http://feedproxy.google.com/~r/Wallabag/~3/helloworld', @@ -917,6 +982,7 @@ class ContentProxyTest extends TestCase 'https://example.org/hello-wallabag', null, 'example.org', + true, ], 'feedproxy ignore list test with origin url already set' => [ 'http://feedproxy.google.com/~r/Wallabag/~3/helloworld', @@ -925,6 +991,7 @@ class ContentProxyTest extends TestCase 'https://example.org/hello-wallabag', 'https://example.org/this-is-source', 'example.org', + true, ], 'lemonde ignore pattern test' => [ 'http://www.lemonde.fr/tiny/url', @@ -933,6 +1000,7 @@ class ContentProxyTest extends TestCase 'http://example.com/hello-world', null, 'example.com', + true, ], ]; } @@ -940,13 +1008,18 @@ class ContentProxyTest extends TestCase /** * @dataProvider dataForChangedUrl */ - public function testWithChangedUrl($entry_url, $origin_url, $content_url, $expected_entry_url, $expected_origin_url, $expected_domain) + public function testWithChangedUrl($entry_url, $origin_url, $content_url, $expected_entry_url, $expected_origin_url, $expected_domain, $processor_result) { $tagger = $this->getTaggerMock(); $tagger->expects($this->once()) ->method('tag'); - $proxy = new ContentProxy((new Graby()), $tagger, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true); + $ruleBasedIgnoreOriginProcessor = $this->getRuleBasedIgnoreOriginProcessorMock(); + $ruleBasedIgnoreOriginProcessor->expects($this->once()) + ->method('process') + ->willReturn($processor_result); + + $proxy = new ContentProxy((new Graby()), $tagger, $ruleBasedIgnoreOriginProcessor, $this->getValidator(), $this->getLogger(), $this->fetchingErrorMessage, true); $entry = new Entry(new User()); $entry->setOriginUrl($origin_url); $proxy->updateEntry( @@ -1015,6 +1088,14 @@ class ContentProxyTest extends TestCase ->getMock(); } + private function getRuleBasedIgnoreOriginProcessorMock() + { + return $this->getMockBuilder(RuleBasedIgnoreOriginProcessor::class) + ->setMethods(['process']) + ->disableOriginalConstructor() + ->getMock(); + } + private function getLogger() { return new NullLogger(); -- cgit v1.2.3 From 7408a6cb687959eac6bc7a2ed370695bbf88b6d6 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Tue, 27 Aug 2019 00:03:30 +0200 Subject: Add controller, views and translations for ignore origin instance rules Signed-off-by: Kevin Decherf --- .../IgnoreOriginInstanceRuleControllerTest.php | 148 +++++++++++++++++++++ 1 file changed, 148 insertions(+) create mode 100644 tests/Wallabag/CoreBundle/Controller/IgnoreOriginInstanceRuleControllerTest.php (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Controller/IgnoreOriginInstanceRuleControllerTest.php b/tests/Wallabag/CoreBundle/Controller/IgnoreOriginInstanceRuleControllerTest.php new file mode 100644 index 00000000..9783cd25 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Controller/IgnoreOriginInstanceRuleControllerTest.php @@ -0,0 +1,148 @@ +logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/ignore-origin-instance-rules/'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('ignore_origin_instance_rule.description', $body); + $this->assertContains('ignore_origin_instance_rule.list.create_new_one', $body); + } + + public function testIgnoreOriginInstanceRuleCreationEditionDeletion() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + // Creation + $crawler = $client->request('GET', '/ignore-origin-instance-rules/new'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('ignore_origin_instance_rule.new_ignore_origin_instance_rule', $body); + $this->assertContains('ignore_origin_instance_rule.form.back_to_list', $body); + + $form = $crawler->filter('button[id=ignore_origin_instance_rule_save]')->form(); + + $data = [ + 'ignore_origin_instance_rule[rule]' => 'host = "foo.example.com"', + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.ignore_origin_instance_rule.notice.added', $crawler->filter('body')->extract(['_text'])[0]); + + // Edition + $editLink = $crawler->filter('div[id=content] table a')->last()->link(); + + $crawler = $client->click($editLink); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $this->assertContains('foo.example.com', $crawler->filter('form[name=ignore_origin_instance_rule] input[type=text]')->extract(['value'])[0]); + + $body = $crawler->filter('body')->extract(['_text'])[0]; + + $this->assertContains('ignore_origin_instance_rule.edit_ignore_origin_instance_rule', $body); + $this->assertContains('ignore_origin_instance_rule.form.back_to_list', $body); + + $form = $crawler->filter('button[id=ignore_origin_instance_rule_save]')->form(); + + $data = [ + 'ignore_origin_instance_rule[rule]' => 'host = "bar.example.com"', + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.ignore_origin_instance_rule.notice.updated', $crawler->filter('body')->extract(['_text'])[0]); + + $editLink = $crawler->filter('div[id=content] table a')->last()->link(); + + $crawler = $client->click($editLink); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $this->assertContains('bar.example.com', $crawler->filter('form[name=ignore_origin_instance_rule] input[type=text]')->extract(['value'])[0]); + + $deleteForm = $crawler->filter('body')->selectButton('ignore_origin_instance_rule.form.delete')->form(); + + $client->submit($deleteForm, []); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $crawler = $client->followRedirect(); + + $this->assertContains('flashes.ignore_origin_instance_rule.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); + } + + public function dataForIgnoreOriginInstanceRuleCreationFail() + { + return [ + [ + [ + 'ignore_origin_instance_rule[rule]' => 'foo = "bar"', + ], + [ + 'The variable', + 'does not exist.', + ], + ], + [ + [ + 'ignore_origin_instance_rule[rule]' => '_all != "none"', + ], + [ + 'The operator', + 'does not exist.', + ], + ], + ]; + } + + /** + * @dataProvider dataForIgnoreOriginInstanceRuleCreationFail + */ + public function testIgnoreOriginInstanceRuleCreationFail($data, $messages) + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/ignore-origin-instance-rules/new'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('button[id=ignore_origin_instance_rule_save]')->form(); + + $crawler = $client->submit($form, $data); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); + + foreach ($messages as $message) { + $this->assertContains($message, $body[0]); + } + } +} -- cgit v1.2.3 From 71f7e58fbd84e1d15c7a405a3c5872adb937dc37 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Tue, 10 Mar 2020 22:22:51 +0100 Subject: tests: add a NetworkCalls group for tests making network calls Excluding this group can decrease the run time of tests during development. Signed-off-by: Kevin Decherf --- .../CoreBundle/Command/ReloadEntryCommandTest.php | 6 +++++ .../CoreBundle/Controller/ConfigControllerTest.php | 3 +++ .../CoreBundle/Controller/EntryControllerTest.php | 26 +++++++++++++++++----- 3 files changed, 30 insertions(+), 5 deletions(-) (limited to 'tests/Wallabag/CoreBundle') diff --git a/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php b/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php index c4bd6dac..a0e2939c 100644 --- a/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php +++ b/tests/Wallabag/CoreBundle/Command/ReloadEntryCommandTest.php @@ -45,6 +45,9 @@ class ReloadEntryCommandTest extends WallabagCoreTestCase $this->getEntityManager()->flush(); } + /** + * @group NetworkCalls + */ public function testRunReloadEntryCommand() { $application = new Application($this->getClient()->getKernel()); @@ -70,6 +73,9 @@ class ReloadEntryCommandTest extends WallabagCoreTestCase $this->assertContains('Done', $tester->getDisplay()); } + /** + * @group NetworkCalls + */ public function testRunReloadEntryWithUsernameCommand() { $application = new Application($this->getClient()->getKernel()); diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php index 40a1aa97..92d22267 100644 --- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php @@ -812,6 +812,9 @@ class ConfigControllerTest extends WallabagCoreTestCase $em->flush(); } + /** + * @group NetworkCalls + */ public function testDeleteAccount() { $client = $this->getClient(); diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 5b806830..0aa562d8 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -40,6 +40,9 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertContains('login', $client->getResponse()->headers->get('location')); } + /** + * @group NetworkCalls + */ public function testQuickstart() { $this->logInAs('empty'); @@ -87,6 +90,9 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertCount(1, $crawler->filter('form[name=entry]')); } + /** + * @group NetworkCalls + */ public function testPostNewViaBookmarklet() { $this->logInAs('admin'); @@ -131,7 +137,7 @@ class EntryControllerTest extends WallabagCoreTestCase } /** - * This test will require an internet connection. + * @group NetworkCalls */ public function testPostNewOk() { @@ -169,6 +175,9 @@ class EntryControllerTest extends WallabagCoreTestCase $client->getContainer()->get('craue_config')->set('store_article_headers', 0); } + /** + * @group NetworkCalls + */ public function testPostWithMultipleAuthors() { $url = 'https://www.liberation.fr/planete/2017/04/05/donald-trump-et-xi-jinping-tentative-de-flirt-en-floride_1560768'; @@ -229,6 +238,9 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertContains('/view/', $client->getResponse()->getTargetUrl()); } + /** + * @group NetworkCalls + */ public function testPostNewOkUrlExistWithAccent() { $this->logInAs('admin'); @@ -265,7 +277,7 @@ class EntryControllerTest extends WallabagCoreTestCase } /** - * This test will require an internet connection. + * @group NetworkCalls */ public function testPostNewOkUrlExistWithRedirection() { @@ -303,7 +315,7 @@ class EntryControllerTest extends WallabagCoreTestCase } /** - * This test will require an internet connection. + * @group NetworkCalls */ public function testPostNewThatWillBeTagged() { @@ -430,7 +442,7 @@ class EntryControllerTest extends WallabagCoreTestCase } /** - * This test will require an internet connection. + * @group NetworkCalls */ public function testReload() { @@ -1056,6 +1068,9 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertSame(404, $client->getResponse()->getStatusCode()); } + /** + * @group NetworkCalls + */ public function testNewEntryWithDownloadImagesEnabled() { $this->downloadImagesEnabled = true; @@ -1383,6 +1398,7 @@ class EntryControllerTest extends WallabagCoreTestCase /** * @dataProvider dataForLanguage + * @group NetworkCalls */ public function testLanguageValidation($url, $expectedLanguage) { @@ -1414,7 +1430,7 @@ class EntryControllerTest extends WallabagCoreTestCase } /** - * This test will require an internet connection. + * @group NetworkCalls */ public function testRestrictedArticle() { -- cgit v1.2.3