]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Isolated tests 3137/head
authoradev <adev2000@gmail.com>
Mon, 15 May 2017 18:47:59 +0000 (20:47 +0200)
committeradev <adev2000@gmail.com>
Wed, 31 May 2017 14:03:54 +0000 (16:03 +0200)
Use https://github.com/dmaicher/doctrine-test-bundle to have test isolation.

24 files changed:
app/AppKernel.php
app/config/config_test.yml
app/config/parameters_test.yml
app/config/tests/parameters_test.mysql.yml
app/config/tests/parameters_test.pgsql.yml
app/config/tests/parameters_test.sqlite.yml
composer.json
phpunit.xml.dist
src/Wallabag/CoreBundle/Command/InstallCommand.php
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php
tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php
tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php
tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
tests/Wallabag/ApiBundle/WallabagApiTestCase.php
tests/Wallabag/CoreBundle/Command/ExportCommandTest.php
tests/Wallabag/CoreBundle/Command/InstallCommandTest.php
tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php
tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php
tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php
tests/Wallabag/CoreBundle/Controller/TagControllerTest.php
tests/Wallabag/CoreBundle/WallabagCoreTestCase.php
tests/Wallabag/ImportBundle/Command/ImportCommandTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php

index b9293498ff60ccf14079e51d217baf48ebb4b617..c50783a6d6901fce9a25d12e6cd9b479daf935af 100644 (file)
@@ -48,6 +48,10 @@ class AppKernel extends Kernel
             $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
             $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
             $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
+
+            if ('test' === $this->getEnvironment()) {
+                $bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
+            }
         }
 
         return $bundles;
index f5e2c25ead1bca8ccb1b9980fa48657f5c864c76..c620c35995c3afb97febcfeecf1d3e72191a263e 100644 (file)
@@ -29,7 +29,8 @@ doctrine:
         user: "%test_database_user%"
         password: "%test_database_password%"
         charset: "%test_database_charset%"
-        path: "%test_database_path%"
+        path: "%env(TEST_DATABASE_PATH)%"
+
     orm:
         metadata_cache_driver:
             type: service
index 5f2e25bb9bb13ebe021a6e3ca6c00a7d11f51e6b..010785e67c5de8e378ba978602eb96dc60864960 100644 (file)
@@ -5,5 +5,6 @@ parameters:
     test_database_name: null
     test_database_user: null
     test_database_password: null
-    test_database_path: '%kernel.root_dir%/../data/db/wallabag_test.sqlite'
+    test_database_path: "%env(TEST_DATABASE_PATH)%"
+    env(TEST_DATABASE_PATH): "%kernel.root_dir%/../data/db/wallabag_test.sqlite"
     test_database_charset: utf8
index bca2d466364588c62dd13a4d2649d3708a2403d8..36b227fbc800f11ce6dbebb0a9d8eade68d728be 100644 (file)
@@ -6,4 +6,5 @@ parameters:
     test_database_user: root
     test_database_password: ~
     test_database_path: ~
+    env(TEST_DATABASE_PATH): ~
     test_database_charset: utf8mb4
index 3e18d4a0395f41fedced20f96fe9ee6241cb9e64..60f51df6d84e0c6631d15de618726bd294c4acfd 100644 (file)
@@ -6,4 +6,5 @@ parameters:
     test_database_user: travis
     test_database_password: ~
     test_database_path: ~
+    env(TEST_DATABASE_PATH): ~
     test_database_charset: utf8
index b8a5f41a709020ae1e59a3d47f15dd10f47732a5..5c731bf54f6bcc704e45b8910d68bb005876a05f 100644 (file)
@@ -5,5 +5,8 @@ parameters:
     test_database_name: ~
     test_database_user: ~
     test_database_password: ~
-    test_database_path: "%kernel.root_dir%/../data/db/wallabag_test.sqlite"
+    # Using an environnement variable in order to avoid the error "attempt to write a readonly database"
+    # when the schema is dropped then recreate
+    test_database_path: "%env(TEST_DATABASE_PATH)%"
+    env(TEST_DATABASE_PATH): "%kernel.root_dir%/../data/db/wallabag_test.sqlite"
     test_database_charset: utf8
index d8c58de28d9800174e7c72dfe29ce8a69d4358cc..60de962c37c8b517b1b79438f113e910ef137665 100644 (file)
         "doctrine/doctrine-fixtures-bundle": "~2.2",
         "doctrine/data-fixtures": "~1.1.1",
         "sensio/generator-bundle": "^3.0",
-        "symfony/phpunit-bridge": "^3.0",
+        "symfony/phpunit-bridge": "^3.3",
         "friendsofphp/php-cs-fixer": "~1.9",
-        "m6web/redis-mock": "^2.0"
+        "m6web/redis-mock": "^2.0",
+        "dama/doctrine-test-bundle": "^1.0"
     },
     "scripts": {
         "post-cmd": [
index 3216b6ffb40818fd4645cc2a15b8ceff68b21920..8f5285e65b9ff196dc17faac326e5b41b54fc41a 100644 (file)
@@ -30,4 +30,8 @@
             </exclude>
         </whitelist>
     </filter>
+
+    <listeners>
+        <listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" />
+    </listeners>
 </phpunit>
index 0d9364f6aa4fb30a2c629f033126e254dbb5d722..d9608246e47da188e496a5b7b6874987010cdc39 100644 (file)
@@ -499,20 +499,18 @@ class InstallCommand extends ContainerAwareCommand
         $output = new BufferedOutput();
         $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
 
+        // PDO does not always close the connection after Doctrine commands.
+        // See https://github.com/symfony/symfony/issues/11750.
+        $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
+
         if (0 !== $exitCode) {
             $this->getApplication()->setAutoExit(true);
 
-            $this->defaultOutput->writeln('');
-            $this->defaultOutput->writeln('<error>The command "'.$command.'" generates some errors: </error>');
-            $this->defaultOutput->writeln($output->fetch());
-
-            die();
+            throw new \RuntimeException(
+                'The command "'.$command."\" generates some errors: \n\n"
+                .$output->fetch());
         }
 
-        // PDO does not always close the connection after Doctrine commands.
-        // See https://github.com/symfony/symfony/issues/11750.
-        $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
-
         return $this;
     }
 
index 1a80cc1ac12f61cd1d9512bd4d9177636b55e4f6..0e61c642039a98c4ced84493130a6ab4e61c4148 100644 (file)
@@ -250,7 +250,7 @@ class ConfigController extends Controller
             case 'entries':
                 // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff
                 // otherwise they won't be removed ...
-                if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
+                if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
                     $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
                 }
 
@@ -262,7 +262,7 @@ class ConfigController extends Controller
                     ->removeAllByUserId($this->getUser()->getId());
                 break;
             case 'archived':
-                if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
+                if ($this->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
                     $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId());
                 }
 
index 3b4c4cf9fedfdd8d8e5ff1ffd141336873a92a68..5e6af8cc5bc341a13154697f11f0d4af3f676be9 100644 (file)
@@ -45,9 +45,8 @@ class SQLiteCascadeDeleteSubscriber implements EventSubscriber
     public function preRemove(LifecycleEventArgs $args)
     {
         $entity = $args->getEntity();
-
-        if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver ||
-            !$entity instanceof Entry) {
+        if (!$this->doctrine->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
+            || !$entity instanceof Entry) {
             return;
         }
 
index 6659443ba44ea461f3be2280a9a3341e9ab7904a..d37cbbf9b04d360b464c83b5dbb54b821f103fe2 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\Wallabag\ApiBundle\Controller;
 
 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use Wallabag\ApiBundle\Entity\Client;
 
 class DeveloperControllerTest extends WallabagCoreTestCase
 {
@@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $this->assertContains('My app', $alert[0]);
     }
 
-    /**
-     * @depends testCreateClient
-     */
     public function testCreateToken()
     {
         $client = $this->getClient();
-        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
-        $apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
+        $apiClient = $this->createApiClientForUser('admin');
 
         $client->request('POST', '/oauth/v2/token', [
             'grant_type' => 'password',
@@ -83,6 +80,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
     public function testRemoveClient()
     {
         $client = $this->getClient();
+        $adminApiClient = $this->createApiClientForUser('admin');
         $em = $client->getContainer()->get('doctrine.orm.entity_manager');
 
         // Try to remove an admin's client with a wrong user
@@ -90,12 +88,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $client->request('GET', '/developer');
         $this->assertContains('no_client', $client->getResponse()->getContent());
 
-        // get an ID of a admin's client
-        $this->logInAs('admin');
-        $nbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
-
         $this->logInAs('bob');
-        $client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId());
+        $client->request('GET', '/developer/client/delete/'.$adminApiClient->getId());
         $this->assertEquals(403, $client->getResponse()->getStatusCode());
 
         // Try to remove the admin's client with the good user
@@ -111,7 +105,29 @@ class DeveloperControllerTest extends WallabagCoreTestCase
         $client->click($link);
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
-        $newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
-        $this->assertGreaterThan(count($newNbClients), count($nbClients));
+        $this->assertNull(
+            $em->getRepository('WallabagApiBundle:Client')->find($adminApiClient->getId()),
+            'The client should have been removed'
+        );
+    }
+
+    /**
+     * @param string $username
+     *
+     * @return Client
+     */
+    private function createApiClientForUser($username)
+    {
+        $client = $this->getClient();
+        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
+        $userManager = $client->getContainer()->get('fos_user.user_manager');
+        $user = $userManager->findUserBy(array('username' => $username));
+        $apiClient = new Client($user);
+        $apiClient->setName('My app');
+        $apiClient->setAllowedGrantTypes(['password']);
+        $em->persist($apiClient);
+        $em->flush();
+
+        return $apiClient;
     }
 }
index bf7d373aa94d57f8716ed59f5f6d02f37ccd3b6f..1ecd03fb96c9173bebd43f0a64930a549758f5b3 100644 (file)
@@ -3,8 +3,10 @@
 namespace Tests\Wallabag\ApiBundle\Controller;
 
 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
+use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Helper\ContentProxy;
+use Wallabag\UserBundle\Entity\User;
 
 class EntryRestControllerTest extends WallabagApiTestCase
 {
@@ -801,22 +803,28 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
     public function testDeleteEntriesTagsListAction()
     {
-        $entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $entry = new Entry($em->getReference(User::class, 1));
+        $entry->setUrl('http://0.0.0.0/test-entry');
+        $entry->addTag((new Tag())->setLabel('foo-tag'));
+        $entry->addTag((new Tag())->setLabel('bar-tag'));
+        $em->persist($entry);
+        $em->flush();
 
-        $tags = $entry->getTags();
-
-        $this->assertCount(4, $tags);
+        $em->clear();
 
         $list = [
             [
-                'url' => 'http://0.0.0.0/entry4',
-                'tags' => 'new tag 1, new tag 2',
+                'url' => 'http://0.0.0.0/test-entry',
+                'tags' => 'foo-tag, bar-tag',
             ],
         ];
 
         $this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list));
+        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
+
+        $entry = $em->getRepository('WallabagCoreBundle:Entry')->find($entry->getId());
+        $this->assertCount(0, $entry->getTags());
     }
 
     public function testPostEntriesListAction()
@@ -841,9 +849,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
 
     public function testDeleteEntriesListAction()
     {
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $em->persist((new Entry($em->getReference(User::class, 1)))->setUrl('http://0.0.0.0/test-entry1'));
+
+        $em->flush();
+        $em->clear();
         $list = [
-            'http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html',
-            'http://0.0.0.0/entry3',
+            'http://0.0.0.0/test-entry1',
+            'http://0.0.0.0/test-entry-not-exist',
         ];
 
         $this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
@@ -853,10 +866,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
         $this->assertTrue($content[0]['entry']);
-        $this->assertEquals('http://www.lemonde.fr/musiques/article/2017/04/23/loin-de-la-politique-le-printemps-de-bourges-retombe-en-enfance_5115862_1654986.html', $content[0]['url']);
+        $this->assertEquals('http://0.0.0.0/test-entry1', $content[0]['url']);
 
         $this->assertFalse($content[1]['entry']);
-        $this->assertEquals('http://0.0.0.0/entry3', $content[1]['url']);
+        $this->assertEquals('http://0.0.0.0/test-entry-not-exist', $content[1]['url']);
     }
 
     public function testLimitBulkAction()
index 90b132ebecbf4600c1d341d6f7027ad76cfbffca..7f69bd67ca985f4008d0219582088a422014c5a4 100644 (file)
@@ -22,36 +22,35 @@ class TagRestControllerTest extends WallabagApiTestCase
         return end($content);
     }
 
-    /**
-     * @depends testGetUserTags
-     */
-    public function testDeleteUserTag($tag)
+    public function testDeleteUserTag()
     {
-        $tagName = $tag['label'];
+        $tagLabel = 'tagtest';
+        $tag = new Tag();
+        $tag->setLabel($tagLabel);
+
+        $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
+        $em->persist($tag);
+        $em->flush();
+        $em->clear();
 
-        $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
+        $this->client->request('DELETE', '/api/tags/'.$tag->getId().'.json');
 
         $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
 
         $content = json_decode($this->client->getResponse()->getContent(), true);
 
         $this->assertArrayHasKey('label', $content);
-        $this->assertEquals($tag['label'], $content['label']);
-        $this->assertEquals($tag['slug'], $content['slug']);
+        $this->assertEquals($tag->getLabel(), $content['label']);
+        $this->assertEquals($tag->getSlug(), $content['slug']);
 
-        $entries = $this->client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findAllByTagId($this->user->getId(), $tag['id']);
+        $entries = $em->getRepository('WallabagCoreBundle:Entry')
+            ->findAllByTagId($this->user->getId(), $tag->getId());
 
         $this->assertCount(0, $entries);
 
-        $tag = $this->client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Tag')
-            ->findOneByLabel($tagName);
+        $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
 
-        $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
+        $this->assertNull($tag, $tagLabel.' was removed because it begun an orphan tag');
     }
 
     public function dataForDeletingTagByLabel()
index a67655c8620abcb5266eaabe4a8119c0e2b4607c..8709da70858e3778f8dbef191185da20e4d6491c 100644 (file)
@@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie;
 abstract class WallabagApiTestCase extends WebTestCase
 {
     /**
-     * @var Client
+     * @var \Symfony\Bundle\FrameworkBundle\Client
      */
     protected $client = null;
 
@@ -23,7 +23,7 @@ abstract class WallabagApiTestCase extends WebTestCase
     }
 
     /**
-     * @return Client
+     * @return \Symfony\Bundle\FrameworkBundle\Client
      */
     protected function createAuthorizedClient()
     {
index 284efac4b6a591e4a22a2e198d42c593b3233fac..2eebf39b408290f3647d0db63bca8a870ae0ed4d 100644 (file)
@@ -55,7 +55,7 @@ class ExportCommandTest extends WallabagCoreTestCase
             'username' => 'admin',
         ]);
 
-        $this->assertContains('Exporting 6 entrie(s) for user « admin »... Done', $tester->getDisplay());
+        $this->assertContains('Exporting 5 entrie(s) for user « admin »... Done', $tester->getDisplay());
         $this->assertFileExists('admin-export.json');
     }
 
index 71c2ffc633917c74a5d00016cd30d704932c1f5c..94fc0b9490679a87e039d78b41328587c5afa033 100644 (file)
@@ -2,8 +2,11 @@
 
 namespace Tests\Wallabag\CoreBundle\Command;
 
+use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
 use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
 use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
+use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
+use Doctrine\DBAL\Platforms\SqlitePlatform;
 use Symfony\Bundle\FrameworkBundle\Console\Application;
 use Symfony\Component\Console\Input\ArrayInput;
 use Symfony\Component\Console\Output\NullOutput;
@@ -18,7 +21,9 @@ class InstallCommandTest extends WallabagCoreTestCase
     {
         parent::setUp();
 
-        if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOPgSql\Driver) {
+        /** @var \Doctrine\DBAL\Connection $connection */
+        $connection = $this->getClient()->getContainer()->get('doctrine')->getConnection();
+        if ($connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
             /*
              * LOG:  statement: CREATE DATABASE "wallabag"
              * ERROR:  source database "template1" is being accessed by other users
@@ -30,34 +35,44 @@ class InstallCommandTest extends WallabagCoreTestCase
              */
             $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
         }
-    }
 
-    /**
-     * Ensure next tests will have a clean database.
-     */
-    public static function tearDownAfterClass()
-    {
-        $application = new Application(static::$kernel);
-        $application->setAutoExit(false);
+        if ($connection->getDatabasePlatform() instanceof SqlitePlatform) {
+            // Environnement variable useful only for sqlite to avoid the error "attempt to write a readonly database"
+            // We can't define always this environnement variable because pdo_mysql seems to use it
+            // and we have the error:
+            // SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
+            // check the manual that corresponds to your MariaDB server version for the right syntax to use
+            // near '/tmp/wallabag_testTYj1kp' at line 1
+            $databasePath = tempnam(sys_get_temp_dir(), 'wallabag_test');
+            putenv("TEST_DATABASE_PATH=$databasePath");
+
+            // The environnement has been changed, recreate the client in order to update connection
+            parent::setUp();
+        }
 
-        $application->run(new ArrayInput([
-            'command' => 'doctrine:schema:drop',
-            '--no-interaction' => true,
-            '--force' => true,
-            '--env' => 'test',
-        ]), new NullOutput());
+        // disable doctrine-test-bundle
+        StaticDriver::setKeepStaticConnections(false);
 
-        $application->run(new ArrayInput([
-            'command' => 'doctrine:schema:create',
-            '--no-interaction' => true,
-            '--env' => 'test',
-        ]), new NullOutput());
+        $this->resetDatabase($this->getClient());
+    }
 
-        $application->run(new ArrayInput([
-            'command' => 'doctrine:fixtures:load',
-            '--no-interaction' => true,
-            '--env' => 'test',
-        ]), new NullOutput());
+    public function tearDown()
+    {
+        $databasePath = getenv('TEST_DATABASE_PATH');
+        // Remove variable environnement
+        putenv('TEST_DATABASE_PATH');
+        if ($databasePath && file_exists($databasePath)) {
+            unlink($databasePath);
+        } else {
+            // Create a new client to avoid the error:
+            // Transaction commit failed because the transaction has been marked for rollback only.
+            $client = static::createClient();
+            $this->resetDatabase($client);
+        }
+
+        // enable doctrine-test-bundle
+        StaticDriver::setKeepStaticConnections(true);
+        parent::tearDown();
     }
 
     public function testRunInstallCommand()
@@ -120,7 +135,7 @@ class InstallCommandTest extends WallabagCoreTestCase
     {
         // skipped SQLite check when database is removed because while testing for the connection,
         // the driver will create the file (so the database) before testing if database exist
-        if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) {
+        if ($this->getClient()->getContainer()->get('doctrine')->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform) {
             $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
         }
 
index 35888f167f8078545105999e944d1f11ac8a5e4c..5bc815ee6f455197ae4acea97f723a5c1d097efc 100644 (file)
@@ -67,8 +67,17 @@ class ConfigControllerTest extends WallabagCoreTestCase
     public function testChangeReadingSpeed()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/test-entry1')
+            ->setReadingTime(22);
+        $this->getEntityManager()->persist($entry);
+
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
+
         $crawler = $client->request('GET', '/unread/list');
         $form = $crawler->filter('button[id=submit-filter]')->form();
         $dataFilters = [
@@ -409,6 +418,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
     public function testTaggingRuleCreation()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/config');
@@ -939,6 +949,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
     public function testSwitchViewMode()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $client->request('GET', '/unread/list');
index 116e5f323bbfa330154fc8b42efb8e1abe59f23f..cc7b3672c73e6ddc98f6d7ce33a73c7cbdae52ee 100644 (file)
@@ -71,6 +71,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testGetNew()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/new');
@@ -84,6 +85,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testPostNewViaBookmarklet()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/');
@@ -195,6 +197,12 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testPostNewOkUrlExist()
     {
         $this->logInAs('admin');
+
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/new');
@@ -288,7 +296,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $tags = $entry->getTags();
 
         $this->assertCount(2, $tags);
-        $this->assertEquals('wallabag', $tags[0]->getLabel());
+        $this->assertContains('wallabag', $tags);
 
         $em->remove($entry);
         $em->flush();
@@ -317,7 +325,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $tags = $entry->getTags();
 
         $this->assertCount(2, $tags);
-        $this->assertEquals('wallabag', $tags[1]->getLabel());
+        $this->assertContains('wallabag', $tags);
 
         $em->remove($entry);
         $em->flush();
@@ -364,24 +372,23 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->assertEquals('/all/list', $client->getResponse()->getTargetUrl());
     }
 
-    /**
-     * @depends testPostNewOk
-     */
     public function testView()
     {
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://example.com/foo');
+        $entry->setTitle('title foo');
+        $entry->setContent('foo bar baz');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
-        $crawler = $client->request('GET', '/view/'.$content->getId());
+        $crawler = $client->request('GET', '/view/'.$entry->getId());
 
         $this->assertEquals(200, $client->getResponse()->getStatusCode());
         $this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text']));
-        $this->assertContains($content->getTitle(), $body[0]);
+        $this->assertContains($entry->getTitle(), $body[0]);
     }
 
     /**
@@ -394,27 +401,23 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $em = $client->getContainer()
-            ->get('doctrine.orm.entity_manager');
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setTitle('title foo');
+        $entry->setContent('');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
-        $content = $em
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
-
-        // empty content
-        $content->setContent('');
-        $em->persist($content);
-        $em->flush();
-
-        $client->request('GET', '/reload/'.$content->getId());
+        $client->request('GET', '/reload/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
-        $content = $em
+        $entry = $this->getEntityManager()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->find($content->getId());
+            ->find($entry->getId());
 
-        $this->assertNotEmpty($content->getContent());
+        $this->assertNotEmpty($entry->getContent());
     }
 
     /**
@@ -425,32 +428,21 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $em = $client->getContainer()
-            ->get('doctrine.orm.entity_manager');
-
-        $content = $em
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
-
-        // put a known failed url
-        $content->setUrl('http://0.0.0.0/failed.html');
-        $em->persist($content);
-        $em->flush();
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/failed.html');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
-        $client->request('GET', '/reload/'.$content->getId());
+        $client->request('GET', '/reload/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
         // force EntityManager to clear previous entity
         // otherwise, retrieve the same entity will retrieve change from the previous request :0
-        $em->clear();
-        $newContent = $em
+        $this->getEntityManager()->clear();
+        $newContent = $this->getEntityManager()
             ->getRepository('WallabagCoreBundle:Entry')
-            ->find($content->getId());
-
-        $newContent->setUrl($this->url);
-        $em->persist($newContent);
-        $em->flush();
+            ->find($entry->getId());
 
         $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent());
     }
@@ -460,12 +452,12 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
-        $crawler = $client->request('GET', '/edit/'.$content->getId());
+        $crawler = $client->request('GET', '/edit/'.$entry->getId());
 
         $this->assertEquals(200, $client->getResponse()->getStatusCode());
 
@@ -478,12 +470,12 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
-        $crawler = $client->request('GET', '/edit/'.$content->getId());
+        $crawler = $client->request('GET', '/edit/'.$entry->getId());
 
         $this->assertEquals(200, $client->getResponse()->getStatusCode());
 
@@ -508,19 +500,20 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
-        $client->request('GET', '/archive/'.$content->getId());
+        $client->request('GET', '/archive/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
         $res = $client->getContainer()
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
-            ->find($content->getId());
+            ->find($entry->getId());
 
         $this->assertEquals($res->isArchived(), true);
     }
@@ -530,19 +523,20 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
-        $client->request('GET', '/star/'.$content->getId());
+        $client->request('GET', '/star/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
         $res = $client->getContainer()
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
-            ->findOneById($content->getId());
+            ->findOneById($entry->getId());
 
         $this->assertEquals($res->isStarred(), true);
     }
@@ -552,16 +546,16 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
-        $client->request('GET', '/delete/'.$content->getId());
+        $client->request('GET', '/delete/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
-        $client->request('GET', '/delete/'.$content->getId());
+        $client->request('GET', '/delete/'.$entry->getId());
 
         $this->assertEquals(404, $client->getResponse()->getStatusCode());
     }
@@ -627,7 +621,13 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testFilterOnReadingTime()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setReadingTime(22);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
 
         $crawler = $client->request('GET', '/unread/list');
 
@@ -666,9 +666,20 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testFilterOnReadingTimeOnlyUpper()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
-        $crawler = $client->request('GET', '/unread/list');
+        $crawler = $client->request('GET', '/all/list');
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
+
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setReadingTime(23);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
+        $crawler = $client->request('GET', '/all/list');
+        $this->assertCount(6, $crawler->filter('div[class=entry]'));
 
         $form = $crawler->filter('button[id=submit-filter]')->form();
 
@@ -678,12 +689,13 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertCount(3, $crawler->filter('div[class=entry]'));
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
     }
 
     public function testFilterOnReadingTimeOnlyLower()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/unread/list');
@@ -696,12 +708,22 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertCount(4, $crawler->filter('div[class=entry]'));
+        $this->assertCount(0, $crawler->filter('div[class=entry]'));
+
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setReadingTime(23);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
+        $crawler = $client->submit($form, $data);
+        $this->assertCount(1, $crawler->filter('div[class=entry]'));
     }
 
     public function testFilterOnUnreadStatus()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/all/list');
@@ -714,12 +736,23 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
+        $this->assertCount(4, $crawler->filter('div[class=entry]'));
+
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setArchived(false);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
+        $crawler = $client->submit($form, $data);
+
         $this->assertCount(5, $crawler->filter('div[class=entry]'));
     }
 
     public function testFilterOnCreationDate()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/unread/list');
@@ -733,7 +766,7 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertCount(6, $crawler->filter('div[class=entry]'));
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
 
         $data = [
             'entry_filter[createdAt][left_date]' => date('d/m/Y'),
@@ -742,7 +775,7 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertCount(6, $crawler->filter('div[class=entry]'));
+        $this->assertCount(5, $crawler->filter('div[class=entry]'));
 
         $data = [
             'entry_filter[createdAt][left_date]' => '01/01/1970',
@@ -786,6 +819,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testFilterOnDomainName()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/unread/list');
@@ -818,6 +852,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testFilterOnStatus()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/unread/list');
@@ -839,6 +874,7 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testPreviewPictureFilter()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
         $crawler = $client->request('GET', '/unread/list');
@@ -846,14 +882,21 @@ class EntryControllerTest extends WallabagCoreTestCase
         $form['entry_filter[previewPicture]']->tick();
 
         $crawler = $client->submit($form);
-        $this->assertCount(2, $crawler->filter('div[class=entry]'));
+        $this->assertCount(1, $crawler->filter('div[class=entry]'));
     }
 
     public function testFilterOnLanguage()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setLanguage('fr');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         $crawler = $client->request('GET', '/unread/list');
         $form = $crawler->filter('button[id=submit-filter]')->form();
         $data = [
@@ -877,10 +920,14 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findOneByUser($this->getLoggedInUserId());
+        // sharing is enabled
+        $client->getContainer()->get('craue_config')->set('share_public', 1);
+
+        $content = new Entry($this->getLoggedInUser());
+        $content->setUrl($this->url);
+        $this->getEntityManager()->persist($content);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
         // no uid
         $client->request('GET', '/share/'.$content->getUid());
@@ -970,6 +1017,20 @@ class EntryControllerTest extends WallabagCoreTestCase
         $url = 'http://www.20minutes.fr/montpellier/1952003-20161030-video-car-tombe-panne-rugbymen-perpignan-improvisent-melee-route';
         $client->getContainer()->get('craue_config')->set('download_images_enabled', 1);
 
+        $crawler = $client->request('GET', '/new');
+
+        $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+        $form = $crawler->filter('form[name=entry]')->form();
+
+        $data = [
+            'entry[url]' => $url,
+        ];
+
+        $client->submit($form, $data);
+
+        $this->assertEquals(302, $client->getResponse()->getStatusCode());
+
         $content = $client->getContainer()
             ->get('doctrine.orm.entity_manager')
             ->getRepository('WallabagCoreBundle:Entry')
@@ -987,28 +1048,19 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('empty');
         $client = $this->getClient();
 
-        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
-        $user = $em
-            ->getRepository('WallabagUserBundle:User')
-            ->find($this->getLoggedInUserId());
-
-        if (!$user) {
-            $this->markTestSkipped('No user found in db.');
-        }
-
         // Redirect to homepage
-        $config = $user->getConfig();
+        $config = $this->getLoggedInUser()->getConfig();
         $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
-        $em->persist($config);
-        $em->flush();
+        $this->getEntityManager()->persist($config);
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
 
-        $client->request('GET', '/view/'.$content->getId());
-        $client->request('GET', '/archive/'.$content->getId());
+        $this->getEntityManager()->flush();
+
+        $client->request('GET', '/view/'.$entry->getId());
+        $client->request('GET', '/archive/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
         $this->assertEquals('/', $client->getResponse()->headers->get('location'));
@@ -1019,46 +1071,36 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->logInAs('empty');
         $client = $this->getClient();
 
-        $em = $client->getContainer()->get('doctrine.orm.entity_manager');
-        $user = $em
-            ->getRepository('WallabagUserBundle:User')
-            ->find($this->getLoggedInUserId());
-
-        if (!$user) {
-            $this->markTestSkipped('No user found in db.');
-        }
-
         // Redirect to current page
-        $config = $user->getConfig();
+        $config = $this->getLoggedInUser()->getConfig();
         $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
-        $em->persist($config);
-        $em->flush();
+        $this->getEntityManager()->persist($config);
 
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $this->getEntityManager()->persist($entry);
 
-        $client->request('GET', '/view/'.$content->getId());
-        $client->request('GET', '/archive/'.$content->getId());
+        $this->getEntityManager()->flush();
+
+        $client->request('GET', '/view/'.$entry->getId());
+        $client->request('GET', '/archive/'.$entry->getId());
 
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
-        $this->assertContains('/view/'.$content->getId(), $client->getResponse()->headers->get('location'));
+        $this->assertContains('/view/'.$entry->getId(), $client->getResponse()->headers->get('location'));
     }
 
     public function testFilterOnHttpStatus()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
-        $crawler = $client->request('GET', '/new');
-        $form = $crawler->filter('form[name=entry]')->form();
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://www.lemonde.fr/incorrect-url/');
+        $entry->setHttpStatus(404);
+        $this->getEntityManager()->persist($entry);
 
-        $data = [
-            'entry[url]' => 'http://www.lemonde.fr/incorrect-url/',
-        ];
-
-        $client->submit($form, $data);
+        $this->getEntityManager()->flush();
 
         $crawler = $client->request('GET', '/all/list');
         $form = $crawler->filter('button[id=submit-filter]')->form();
@@ -1071,14 +1113,17 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $this->assertCount(1, $crawler->filter('div[class=entry]'));
 
-        $crawler = $client->request('GET', '/new');
-        $form = $crawler->filter('form[name=entry]')->form();
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setHttpStatus(200);
+        $this->getEntityManager()->persist($entry);
 
-        $data = [
-            'entry[url]' => 'http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm',
-        ];
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm');
+        $entry->setHttpStatus(200);
+        $this->getEntityManager()->persist($entry);
 
-        $client->submit($form, $data);
+        $this->getEntityManager()->flush();
 
         $crawler = $client->request('GET', '/all/list');
         $form = $crawler->filter('button[id=submit-filter]')->form();
@@ -1106,8 +1151,15 @@ class EntryControllerTest extends WallabagCoreTestCase
     public function testSearch()
     {
         $this->logInAs('admin');
+        $this->useTheme('baggy');
         $client = $this->getClient();
 
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl($this->url);
+        $entry->setTitle('test');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         // Search on unread list
         $crawler = $client->request('GET', '/unread/list');
 
@@ -1118,35 +1170,37 @@ class EntryControllerTest extends WallabagCoreTestCase
 
         $crawler = $client->submit($form, $data);
 
-        $this->assertCount(5, $crawler->filter('div[class=entry]'));
+        $this->assertCount(4, $crawler->filter('div[class=entry]'));
 
         // Search on starred list
         $crawler = $client->request('GET', '/starred/list');
 
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://localhost/foo/bar');
+        $entry->setTitle('testeur');
+        $entry->setStarred(true);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         $form = $crawler->filter('form[name=search]')->form();
         $data = [
-            'search_entry[term]' => 'title',
+            'search_entry[term]' => 'testeur',
         ];
 
         $crawler = $client->submit($form, $data);
 
         $this->assertCount(1, $crawler->filter('div[class=entry]'));
 
-        // Added new article to test on archive list
-        $crawler = $client->request('GET', '/new');
-        $form = $crawler->filter('form[name=entry]')->form();
-        $data = [
-            'entry[url]' => $this->url,
-        ];
-        $client->submit($form, $data);
-        $content = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
-        $client->request('GET', '/archive/'.$content->getId());
-
         $crawler = $client->request('GET', '/archive/list');
 
+        // Added new article to test on archive list
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/foo/baz/qux');
+        $entry->setTitle('Le manège');
+        $entry->setArchived(true);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         $form = $crawler->filter('form[name=search]')->form();
         $data = [
             'search_entry[term]' => 'manège',
@@ -1155,7 +1209,7 @@ class EntryControllerTest extends WallabagCoreTestCase
         $crawler = $client->submit($form, $data);
 
         $this->assertCount(1, $crawler->filter('div[class=entry]'));
-        $client->request('GET', '/delete/'.$content->getId());
+        $client->request('GET', '/delete/'.$entry->getId());
 
         // test on list of all articles
         $crawler = $client->request('GET', '/all/list');
@@ -1170,6 +1224,13 @@ class EntryControllerTest extends WallabagCoreTestCase
         $this->assertCount(0, $crawler->filter('div[class=entry]'));
 
         // test url search on list of all articles
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://domain/qux');
+        $entry->setTitle('Le manège');
+        $entry->setArchived(true);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+
         $crawler = $client->request('GET', '/all/list');
 
         $form = $crawler->filter('form[name=search]')->form();
index 63f2c829a8eef3bae39ca0b9d69fbf3080e289e9..b38961d391d4dd8abd58489e38f8cdc11a6a667b 100644 (file)
@@ -239,7 +239,7 @@ class ExportControllerTest extends WallabagCoreTestCase
         $this->assertEquals($contentInDB->getLanguage(), $content[0]['language']);
         $this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']);
         $this->assertEquals($contentInDB->getDomainname(), $content[0]['domain_name']);
-        $this->assertEquals(['foo bar', 'baz', 'foot'], $content[0]['tags']);
+        $this->assertEquals(['foo bar', 'baz'], $content[0]['tags']);
     }
 
     public function testXmlExport()
index 80611a87c2010549a31d6672fce7aaf7b654df1c..f9bf7b878c99c340c480c139c56fd176517131ad 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\Wallabag\CoreBundle\Controller;
 
 use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 
 class TagControllerTest extends WallabagCoreTestCase
@@ -24,10 +25,11 @@ class TagControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $entry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/foo');
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
         $crawler = $client->request('GET', '/view/'.$entry->getId());
 
@@ -41,23 +43,15 @@ class TagControllerTest extends WallabagCoreTestCase
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
         // be sure to reload the entry
-        $entry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
-
-        $this->assertEquals(4, count($entry->getTags()));
+        $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
+        $this->assertCount(1, $entry->getTags());
 
         // tag already exists and already assigned
         $client->submit($form, $data);
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
-        $newEntry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->find($entry->getId());
-
-        $this->assertEquals(4, count($newEntry->getTags()));
+        $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
+        $this->assertCount(1, $entry->getTags());
 
         // tag already exists but still not assigned to this entry
         $data = [
@@ -67,12 +61,8 @@ class TagControllerTest extends WallabagCoreTestCase
         $client->submit($form, $data);
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
 
-        $newEntry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->find($entry->getId());
-
-        $this->assertEquals(4, count($newEntry->getTags()));
+        $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
+        $this->assertCount(2, $entry->getTags());
     }
 
     public function testAddMultipleTagToEntry()
@@ -116,15 +106,14 @@ class TagControllerTest extends WallabagCoreTestCase
         $this->logInAs('admin');
         $client = $this->getClient();
 
-        $entry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
-
-        $tag = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Tag')
-            ->findOneByEntryAndTagLabel($entry, $this->tagName);
+        $tag = new Tag();
+        $tag->setLabel($this->tagName);
+        $entry = new Entry($this->getLoggedInUser());
+        $entry->setUrl('http://0.0.0.0/foo');
+        $entry->addTag($tag);
+        $this->getEntityManager()->persist($entry);
+        $this->getEntityManager()->flush();
+        $this->getEntityManager()->clear();
 
         // We make a first request to set an history and test redirection after tag deletion
         $client->request('GET', '/view/'.$entry->getId());
@@ -134,12 +123,8 @@ class TagControllerTest extends WallabagCoreTestCase
         $this->assertEquals(302, $client->getResponse()->getStatusCode());
         $this->assertEquals($entryUri, $client->getResponse()->getTargetUrl());
 
-        // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
-        $entry = $client->getContainer()
-            ->get('doctrine.orm.entity_manager')
-            ->getRepository('WallabagCoreBundle:Entry')
-            ->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
-
+       // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
+        $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
         $this->assertNotContains($this->tagName, $entry->getTags());
 
         $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());
index 7bf4b43ce8f32ee462aaec0187e44df448dcea76..eec6939d77756c33f72f67d90f9db7f55af23ef3 100644 (file)
@@ -2,11 +2,20 @@
 
 namespace Tests\Wallabag\CoreBundle;
 
+use Symfony\Bundle\FrameworkBundle\Client;
+use Symfony\Bundle\FrameworkBundle\Console\Application;
 use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 use Symfony\Component\BrowserKit\Cookie;
+use Symfony\Component\Console\Input\ArrayInput;
+use Symfony\Component\Console\Output\NullOutput;
+use Wallabag\CoreBundle\Entity\Config;
+use Wallabag\UserBundle\Entity\User;
 
 abstract class WallabagCoreTestCase extends WebTestCase
 {
+    /**
+     * @var Client|null
+     */
     private $client = null;
 
     public function getClient()
@@ -21,6 +30,44 @@ abstract class WallabagCoreTestCase extends WebTestCase
         $this->client = static::createClient();
     }
 
+    public function resetDatabase(Client $client)
+    {
+        $application = new Application($client->getKernel());
+        $application->setAutoExit(false);
+
+        $application->run(new ArrayInput([
+            'command' => 'doctrine:schema:drop',
+            '--no-interaction' => true,
+            '--force' => true,
+            '--env' => 'test',
+        ]), new NullOutput());
+
+        $application->run(new ArrayInput([
+            'command' => 'doctrine:schema:create',
+            '--no-interaction' => true,
+            '--env' => 'test',
+        ]), new NullOutput());
+
+        $application->run(new ArrayInput([
+            'command' => 'doctrine:fixtures:load',
+            '--no-interaction' => true,
+            '--env' => 'test',
+        ]), new NullOutput());
+
+        /*
+         * Recreate client to avoid error:
+         *
+         * [Doctrine\DBAL\ConnectionException]
+         * Transaction commit failed because the transaction has been marked for rollback only.
+         */
+        $this->client = static::createClient();
+    }
+
+    public function getEntityManager()
+    {
+        return $this->client->getContainer()->get('doctrine.orm.entity_manager');
+    }
+
     /**
      * Login a user without making a HTTP request.
      * If we make a HTTP request we lose ability to mock service in the container.
@@ -37,7 +84,7 @@ abstract class WallabagCoreTestCase extends WebTestCase
         $firewallName = $container->getParameter('fos_user.firewall_name');
 
         $user = $userManager->findUserBy(array('username' => $username));
-        $loginManager->loginUser($firewallName, $user);
+        $loginManager->logInUser($firewallName, $user);
 
         $session->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
         $session->save();
@@ -65,22 +112,41 @@ abstract class WallabagCoreTestCase extends WebTestCase
     }
 
     /**
-     * Return the user id of the logged in user.
+     * Return the user of the logged in user.
      * You should be sure that you called `logInAs` before.
      *
-     * @return int
+     * @return User
      */
-    public function getLoggedInUserId()
+    public function getLoggedInUser()
     {
         $token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
 
         if (null !== $token) {
-            return $token->getUser()->getId();
+            return $token->getUser();
         }
 
         throw new \RuntimeException('No logged in User.');
     }
 
+    /**
+     * Return the user id of the logged in user.
+     * You should be sure that you called `logInAs` before.
+     *
+     * @return int
+     */
+    public function getLoggedInUserId()
+    {
+        return $this->getLoggedInUser()->getId();
+    }
+
+    public function useTheme($theme)
+    {
+        $config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser());
+        $config->setTheme($theme);
+        $this->getEntityManager()->persist($config);
+        $this->getEntityManager()->flush();
+    }
+
     /**
      * Check if Redis is installed.
      * If not, mark test as skip.
index 7043c3450b73c63d267057c20e13b160840767e4..f507563cc6039b13f54cf03c475343539721c9a8 100644 (file)
@@ -95,6 +95,7 @@ class ImportCommandTest extends WallabagCoreTestCase
             'username' => 1,
             'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json',
             '--useUserId' => true,
+            '--importer' => 'v2',
         ]);
     }
 }
index 18a025226c5c3fdad89977cc632bfae44a0fc126..248932591759aa585c8fd1f18211f16df15661e0 100644 (file)
@@ -119,9 +119,10 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase
                 $this->getLoggedInUserId()
             );
 
-        $this->assertNotEmpty($content->getMimetype(), 'Mimetype for http://www.liberation.fr is ok');
-        $this->assertNotEmpty($content->getPreviewPicture(), 'Preview picture for http://www.liberation.fr is ok');
-        $this->assertNotEmpty($content->getLanguage(), 'Language for http://www.liberation.fr is ok');
+        // empty because it wasn't re-imported
+        $this->assertEmpty($content->getMimetype(), 'Mimetype for http://www.liberation.fr is empty');
+        $this->assertEmpty($content->getPreviewPicture(), 'Preview picture for http://www.liberation.fr is empty');
+        $this->assertEmpty($content->getLanguage(), 'Language for http://www.liberation.fr is empty');
 
         $tags = $content->getTags();
         $this->assertContains('foot', $tags, 'It includes the "foot" tag');