From: adev Date: Mon, 15 May 2017 18:47:59 +0000 (+0200) Subject: Isolated tests X-Git-Tag: 2.3.0~31^2~77^2 X-Git-Url: https://git.immae.eu/?p=github%2Fwallabag%2Fwallabag.git;a=commitdiff_plain;h=7ab5eb9508921d84b4b4ec84a59135d536da748e Isolated tests Use https://github.com/dmaicher/doctrine-test-bundle to have test isolation. --- diff --git a/app/AppKernel.php b/app/AppKernel.php index b9293498..c50783a6 100644 --- a/app/AppKernel.php +++ b/app/AppKernel.php @@ -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; diff --git a/app/config/config_test.yml b/app/config/config_test.yml index f5e2c25e..c620c359 100644 --- a/app/config/config_test.yml +++ b/app/config/config_test.yml @@ -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 diff --git a/app/config/parameters_test.yml b/app/config/parameters_test.yml index 5f2e25bb..010785e6 100644 --- a/app/config/parameters_test.yml +++ b/app/config/parameters_test.yml @@ -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 diff --git a/app/config/tests/parameters_test.mysql.yml b/app/config/tests/parameters_test.mysql.yml index bca2d466..36b227fb 100644 --- a/app/config/tests/parameters_test.mysql.yml +++ b/app/config/tests/parameters_test.mysql.yml @@ -6,4 +6,5 @@ parameters: test_database_user: root test_database_password: ~ test_database_path: ~ + env(TEST_DATABASE_PATH): ~ test_database_charset: utf8mb4 diff --git a/app/config/tests/parameters_test.pgsql.yml b/app/config/tests/parameters_test.pgsql.yml index 3e18d4a0..60f51df6 100644 --- a/app/config/tests/parameters_test.pgsql.yml +++ b/app/config/tests/parameters_test.pgsql.yml @@ -6,4 +6,5 @@ parameters: test_database_user: travis test_database_password: ~ test_database_path: ~ + env(TEST_DATABASE_PATH): ~ test_database_charset: utf8 diff --git a/app/config/tests/parameters_test.sqlite.yml b/app/config/tests/parameters_test.sqlite.yml index b8a5f41a..5c731bf5 100644 --- a/app/config/tests/parameters_test.sqlite.yml +++ b/app/config/tests/parameters_test.sqlite.yml @@ -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 diff --git a/composer.json b/composer.json index d8c58de2..60de962c 100644 --- a/composer.json +++ b/composer.json @@ -90,9 +90,10 @@ "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": [ diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 3216b6ff..8f5285e6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -30,4 +30,8 @@ + + + + diff --git a/src/Wallabag/CoreBundle/Command/InstallCommand.php b/src/Wallabag/CoreBundle/Command/InstallCommand.php index 0d9364f6..d9608246 100644 --- a/src/Wallabag/CoreBundle/Command/InstallCommand.php +++ b/src/Wallabag/CoreBundle/Command/InstallCommand.php @@ -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('The command "'.$command.'" generates some errors: '); - $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; } diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index 1a80cc1a..0e61c642 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -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()); } diff --git a/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php b/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php index 3b4c4cf9..5e6af8cc 100644 --- a/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php +++ b/src/Wallabag/CoreBundle/Event/Subscriber/SQLiteCascadeDeleteSubscriber.php @@ -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; } diff --git a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php index 6659443b..d37cbbf9 100644 --- a/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/DeveloperControllerTest.php @@ -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; } } diff --git a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php index bf7d373a..1ecd03fb 100644 --- a/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/EntryRestControllerTest.php @@ -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() diff --git a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php index 90b132eb..7f69bd67 100644 --- a/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php +++ b/tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php @@ -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() diff --git a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php index a67655c8..8709da70 100644 --- a/tests/Wallabag/ApiBundle/WallabagApiTestCase.php +++ b/tests/Wallabag/ApiBundle/WallabagApiTestCase.php @@ -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() { diff --git a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php index 284efac4..2eebf39b 100644 --- a/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php +++ b/tests/Wallabag/CoreBundle/Command/ExportCommandTest.php @@ -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'); } diff --git a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php index 71c2ffc6..94fc0b94 100644 --- a/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php +++ b/tests/Wallabag/CoreBundle/Command/InstallCommandTest.php @@ -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.'); } diff --git a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php index 35888f16..5bc815ee 100644 --- a/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ConfigControllerTest.php @@ -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'); diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 116e5f32..cc7b3672 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -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(); diff --git a/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php b/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php index 63f2c829..b38961d3 100644 --- a/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/ExportControllerTest.php @@ -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() diff --git a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php index 80611a87..f9bf7b87 100644 --- a/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/TagControllerTest.php @@ -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()); diff --git a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php index 7bf4b43c..eec6939d 100644 --- a/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php +++ b/tests/Wallabag/CoreBundle/WallabagCoreTestCase.php @@ -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. diff --git a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php index 7043c345..f507563c 100644 --- a/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php +++ b/tests/Wallabag/ImportBundle/Command/ImportCommandTest.php @@ -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', ]); } } diff --git a/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php b/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php index 18a02522..24893259 100644 --- a/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php +++ b/tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php @@ -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');