]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
Add ability to import/export tagging rules
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / Controller / ConfigControllerTest.php
index e07c57dd34e7544e100420779bd31c15f184b908..d8b5f38357c39ed4504552ab056122ca44897863 100644 (file)
@@ -1,7 +1,8 @@
 <?php
 
-namespace tests\Wallabag\CoreBundle\Controller;
+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;
@@ -33,7 +34,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $this->assertCount(1, $crawler->filter('button[id=config_save]'));
         $this->assertCount(1, $crawler->filter('button[id=change_passwd_save]'));
         $this->assertCount(1, $crawler->filter('button[id=update_user_save]'));
-        $this->assertCount(1, $crawler->filter('button[id=rss_config_save]'));
+        $this->assertCount(1, $crawler->filter('button[id=feed_config_save]'));
     }
 
     public function testUpdate()
@@ -297,7 +298,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $this->assertContains('flashes.config.notice.user_updated', $alert[0]);
     }
 
-    public function testRssUpdateResetToken()
+    public function testFeedUpdateResetToken()
     {
         $this->logInAs('admin');
         $client = $this->getClient();
@@ -313,7 +314,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         }
 
         $config = $user->getConfig();
-        $config->setRssToken(null);
+        $config->setFeedToken(null);
         $em->persist($config);
         $em->flush();
 
@@ -322,7 +323,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $this->assertSame(200, $client->getResponse()->getStatusCode());
 
         $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
-        $this->assertContains('config.form_rss.no_token', $body[0]);
+        $this->assertContains('config.form_feed.no_token', $body[0]);
 
         $client->request('GET', '/generate-token');
         $this->assertSame(302, $client->getResponse()->getStatusCode());
@@ -330,7 +331,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $crawler = $client->followRedirect();
 
         $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
-        $this->assertNotContains('config.form_rss.no_token', $body[0]);
+        $this->assertContains('config.form_feed.token_reset', $body[0]);
     }
 
     public function testGenerateTokenAjax()
@@ -351,7 +352,23 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $this->assertArrayHasKey('token', $content);
     }
 
-    public function testRssUpdate()
+    public function testRevokeTokenAjax()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $client->request(
+            'GET',
+            '/revoke-token',
+            [],
+            [],
+            ['HTTP_X-Requested-With' => 'XMLHttpRequest']
+        );
+
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
+    }
+
+    public function testFeedUpdate()
     {
         $this->logInAs('admin');
         $client = $this->getClient();
@@ -360,10 +377,10 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $this->assertSame(200, $client->getResponse()->getStatusCode());
 
-        $form = $crawler->filter('button[id=rss_config_save]')->form();
+        $form = $crawler->filter('button[id=feed_config_save]')->form();
 
         $data = [
-            'rss_config[rss_limit]' => 12,
+            'feed_config[feed_limit]' => 12,
         ];
 
         $client->submit($form, $data);
@@ -372,31 +389,31 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->followRedirect();
 
-        $this->assertContains('flashes.config.notice.rss_updated', $crawler->filter('body')->extract(['_text'])[0]);
+        $this->assertContains('flashes.config.notice.feed_updated', $crawler->filter('body')->extract(['_text'])[0]);
     }
 
-    public function dataForRssFailed()
+    public function dataForFeedFailed()
     {
         return [
             [
                 [
-                    'rss_config[rss_limit]' => 0,
+                    'feed_config[feed_limit]' => 0,
                 ],
                 'This value should be 1 or more.',
             ],
             [
                 [
-                    'rss_config[rss_limit]' => 1000000000000,
+                    'feed_config[feed_limit]' => 1000000000000,
                 ],
-                'validator.rss_limit_too_high',
+                'validator.feed_limit_too_high',
             ],
         ];
     }
 
     /**
-     * @dataProvider dataForRssFailed
+     * @dataProvider dataForFeedFailed
      */
-    public function testRssFailed($data, $expectedMessage)
+    public function testFeedFailed($data, $expectedMessage)
     {
         $this->logInAs('admin');
         $client = $this->getClient();
@@ -405,7 +422,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $this->assertSame(200, $client->getResponse()->getStatusCode());
 
-        $form = $crawler->filter('button[id=rss_config_save]')->form();
+        $form = $crawler->filter('button[id=feed_config_save]')->form();
 
         $crawler = $client->submit($form, $data);
 
@@ -849,7 +866,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
         $entryArchived->setContent('Youhou');
         $entryArchived->setTitle('Youhou');
         $entryArchived->addTag($tagArchived);
-        $entryArchived->setArchived(true);
+        $entryArchived->updateArchived(true);
         $em->persist($entryArchived);
 
         $annotationArchived = new Annotation($user);
@@ -965,4 +982,183 @@ class ConfigControllerTest extends WallabagCoreTestCase
 
         $client->request('GET', '/config/view-mode');
     }
+
+    public function testChangeLocaleWithoutReferer()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/locale/de');
+        $client->followRedirect();
+
+        $this->assertSame('de', $client->getRequest()->getLocale());
+        $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
+    }
+
+    public function testChangeLocaleWithReferer()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/login');
+        $client->request('GET', '/locale/de');
+        $client->followRedirect();
+
+        $this->assertSame('de', $client->getRequest()->getLocale());
+        $this->assertSame('de', $client->getContainer()->get('session')->get('_locale'));
+    }
+
+    public function testChangeLocaleToBadLocale()
+    {
+        $client = $this->getClient();
+
+        $client->request('GET', '/login');
+        $client->request('GET', '/locale/yuyuyuyu');
+        $client->followRedirect();
+
+        $this->assertNotSame('yuyuyuyu', $client->getRequest()->getLocale());
+        $this->assertNotSame('yuyuyuyu', $client->getContainer()->get('session')->get('_locale'));
+    }
+
+    public function testUserEnable2faEmail()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/config/otp/email');
+
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+
+        $crawler = $client->followRedirect();
+
+        $this->assertGreaterThan(1, $alert = $crawler->filter('body')->extract(['_text']));
+        $this->assertContains('flashes.config.notice.otp_enabled', $alert[0]);
+
+        // restore user
+        $em = $this->getEntityManager();
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByUsername('admin');
+
+        $this->assertTrue($user->isEmailTwoFactor());
+
+        $user->setEmailTwoFactor(false);
+        $em->persist($user);
+        $em->flush();
+    }
+
+    public function testUserEnable2faGoogle()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/config/otp/app');
+
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
+
+        // restore user
+        $em = $this->getEntityManager();
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByUsername('admin');
+
+        $this->assertTrue($user->isGoogleTwoFactor());
+        $this->assertGreaterThan(0, $user->getBackupCodes());
+
+        $user->setGoogleAuthenticatorSecret(false);
+        $user->setBackupCodes(null);
+        $em->persist($user);
+        $em->flush();
+    }
+
+    public function testUserEnable2faGoogleCancel()
+    {
+        $this->logInAs('admin');
+        $client = $this->getClient();
+
+        $crawler = $client->request('GET', '/config/otp/app');
+
+        $this->assertSame(200, $client->getResponse()->getStatusCode());
+
+        // restore user
+        $em = $this->getEntityManager();
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByUsername('admin');
+
+        $this->assertTrue($user->isGoogleTwoFactor());
+        $this->assertGreaterThan(0, $user->getBackupCodes());
+
+        $crawler = $client->request('GET', '/config/otp/app/cancel');
+
+        $this->assertSame(302, $client->getResponse()->getStatusCode());
+
+        $user = $em
+            ->getRepository('WallabagUserBundle:User')
+            ->findOneByUsername('admin');
+
+        $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());
+    }
 }