From 34be2d5de44ade2a78be73decc0b90a2c1bca720 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Wed, 26 Jun 2019 22:31:47 +0200 Subject: Add ability to import/export tagging rules - Add missing translations - Add some tests - Add `/api/taggingrule/export` API endpoint - Add baggy theme - Add error message when importing tagging rules failed - Also fix all translations (I think we are good now) --- .../Controller/TaggingRuleRestControllerTest.php | 15 +++++ .../CoreBundle/Controller/ConfigControllerTest.php | 64 ++++++++++++++++++++++ .../CoreBundle/fixtures/tagging_rules_admin.json | 4 ++ 3 files changed, 83 insertions(+) create mode 100644 tests/Wallabag/ApiBundle/Controller/TaggingRuleRestControllerTest.php create mode 100644 tests/Wallabag/CoreBundle/fixtures/tagging_rules_admin.json (limited to 'tests') diff --git a/tests/Wallabag/ApiBundle/Controller/TaggingRuleRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TaggingRuleRestControllerTest.php new file mode 100644 index 00000000..b6477256 --- /dev/null +++ b/tests/Wallabag/ApiBundle/Controller/TaggingRuleRestControllerTest.php @@ -0,0 +1,15 @@ +client->request('GET', '/api/taggingrule/export'); + $this->assertSame(200, $this->client->getResponse()->getStatusCode()); + $this->assertSame('application/json', $this->client->getResponse()->headers->get('Content-Type')); + } +} diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php index b9e0bed2..d8b5f383 100644 --- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php @@ -2,6 +2,7 @@ namespace Tests\Wallabag\CoreBundle\Controller; +use Symfony\Component\HttpFoundation\File\UploadedFile; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Wallabag\AnnotationBundle\Entity\Annotation; use Wallabag\CoreBundle\Entity\Config; @@ -1097,4 +1098,67 @@ class ConfigControllerTest extends WallabagCoreTestCase $this->assertFalse($user->isGoogleTwoFactor()); $this->assertEmpty($user->getBackupCodes()); } + + public function testExportTaggingRule() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + ob_start(); + $crawler = $client->request('GET', '/tagging-rule/export'); + ob_end_clean(); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $headers = $client->getResponse()->headers; + $this->assertSame('application/json', $headers->get('content-type')); + $this->assertSame('attachment; filename="tagging_rules_admin.json"', $headers->get('content-disposition')); + $this->assertSame('UTF-8', $headers->get('content-transfer-encoding')); + + $content = json_decode($client->getResponse()->getContent(), true); + + $this->assertCount(4, $content); + $this->assertSame('content matches "spurs"', $content[0]['rule']); + $this->assertSame('sport', $content[0]['tags'][0]); + } + + public function testImportTagginfRuleBadFile() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form(); + + $data = [ + 'upload_tagging_rule_file[file]' => '', + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + } + + public function testImportTagginfRuleFile() + { + $this->logInAs('admin'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/config'); + $form = $crawler->filter('form[name=upload_tagging_rule_file] > button[type=submit]')->form(); + + $file = new UploadedFile(__DIR__ . '/../fixtures/tagging_rules_admin.json', 'tagging_rules_admin.json'); + + $data = [ + 'upload_tagging_rule_file[file]' => $file, + ]; + + $client->submit($form, $data); + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $user = $client->getContainer()->get('fos_user.user_manager.test')->findUserBy(['username' => 'admin']); + $taggingRules = $user->getConfig()->getTaggingRules()->toArray(); + $this->assertCount(5, $taggingRules); + $this->assertSame('title matches "football"', $taggingRules[4]->getRule()); + } } diff --git a/tests/Wallabag/CoreBundle/fixtures/tagging_rules_admin.json b/tests/Wallabag/CoreBundle/fixtures/tagging_rules_admin.json new file mode 100644 index 00000000..a54824e2 --- /dev/null +++ b/tests/Wallabag/CoreBundle/fixtures/tagging_rules_admin.json @@ -0,0 +1,4 @@ +[{ + "rule": "title matches \"football\"", + "tags": ["football"] +}] -- cgit v1.2.3