Isolated tests

Use https://github.com/dmaicher/doctrine-test-bundle to have test isolation.
This commit is contained in:
adev 2017-05-15 20:47:59 +02:00
parent 4423b88c5b
commit 7ab5eb9508
24 changed files with 468 additions and 287 deletions

View file

@ -48,6 +48,10 @@ class AppKernel extends Kernel
$bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle(); $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
$bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle(); $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
$bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(); $bundles[] = new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle();
if ('test' === $this->getEnvironment()) {
$bundles[] = new DAMA\DoctrineTestBundle\DAMADoctrineTestBundle();
}
} }
return $bundles; return $bundles;

View file

@ -29,7 +29,8 @@ doctrine:
user: "%test_database_user%" user: "%test_database_user%"
password: "%test_database_password%" password: "%test_database_password%"
charset: "%test_database_charset%" charset: "%test_database_charset%"
path: "%test_database_path%" path: "%env(TEST_DATABASE_PATH)%"
orm: orm:
metadata_cache_driver: metadata_cache_driver:
type: service type: service

View file

@ -5,5 +5,6 @@ parameters:
test_database_name: null test_database_name: null
test_database_user: null test_database_user: null
test_database_password: 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 test_database_charset: utf8

View file

@ -6,4 +6,5 @@ parameters:
test_database_user: root test_database_user: root
test_database_password: ~ test_database_password: ~
test_database_path: ~ test_database_path: ~
env(TEST_DATABASE_PATH): ~
test_database_charset: utf8mb4 test_database_charset: utf8mb4

View file

@ -6,4 +6,5 @@ parameters:
test_database_user: travis test_database_user: travis
test_database_password: ~ test_database_password: ~
test_database_path: ~ test_database_path: ~
env(TEST_DATABASE_PATH): ~
test_database_charset: utf8 test_database_charset: utf8

View file

@ -5,5 +5,8 @@ parameters:
test_database_name: ~ test_database_name: ~
test_database_user: ~ test_database_user: ~
test_database_password: ~ 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 test_database_charset: utf8

View file

@ -90,9 +90,10 @@
"doctrine/doctrine-fixtures-bundle": "~2.2", "doctrine/doctrine-fixtures-bundle": "~2.2",
"doctrine/data-fixtures": "~1.1.1", "doctrine/data-fixtures": "~1.1.1",
"sensio/generator-bundle": "^3.0", "sensio/generator-bundle": "^3.0",
"symfony/phpunit-bridge": "^3.0", "symfony/phpunit-bridge": "^3.3",
"friendsofphp/php-cs-fixer": "~1.9", "friendsofphp/php-cs-fixer": "~1.9",
"m6web/redis-mock": "^2.0" "m6web/redis-mock": "^2.0",
"dama/doctrine-test-bundle": "^1.0"
}, },
"scripts": { "scripts": {
"post-cmd": [ "post-cmd": [

View file

@ -30,4 +30,8 @@
</exclude> </exclude>
</whitelist> </whitelist>
</filter> </filter>
<listeners>
<listener class="\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitStaticDbConnectionListener" />
</listeners>
</phpunit> </phpunit>

View file

@ -499,20 +499,18 @@ class InstallCommand extends ContainerAwareCommand
$output = new BufferedOutput(); $output = new BufferedOutput();
$exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output); $exitCode = $this->getApplication()->run(new ArrayInput($parameters), $output);
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();
}
// PDO does not always close the connection after Doctrine commands. // PDO does not always close the connection after Doctrine commands.
// See https://github.com/symfony/symfony/issues/11750. // See https://github.com/symfony/symfony/issues/11750.
$this->getContainer()->get('doctrine')->getManager()->getConnection()->close(); $this->getContainer()->get('doctrine')->getManager()->getConnection()->close();
if (0 !== $exitCode) {
$this->getApplication()->setAutoExit(true);
throw new \RuntimeException(
'The command "'.$command."\" generates some errors: \n\n"
.$output->fetch());
}
return $this; return $this;
} }

View file

@ -250,7 +250,7 @@ class ConfigController extends Controller
case 'entries': case 'entries':
// SQLite doesn't care about cascading remove, so we need to manually remove associated stuff // SQLite doesn't care about cascading remove, so we need to manually remove associated stuff
// otherwise they won't be removed ... // 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()); $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId());
} }
@ -262,7 +262,7 @@ class ConfigController extends Controller
->removeAllByUserId($this->getUser()->getId()); ->removeAllByUserId($this->getUser()->getId());
break; break;
case 'archived': 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()); $this->removeAnnotationsForArchivedByUserId($this->getUser()->getId());
} }

View file

@ -45,9 +45,8 @@ class SQLiteCascadeDeleteSubscriber implements EventSubscriber
public function preRemove(LifecycleEventArgs $args) public function preRemove(LifecycleEventArgs $args)
{ {
$entity = $args->getEntity(); $entity = $args->getEntity();
if (!$this->doctrine->getConnection()->getDatabasePlatform() instanceof \Doctrine\DBAL\Platforms\SqlitePlatform
if (!$this->doctrine->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver || || !$entity instanceof Entry) {
!$entity instanceof Entry) {
return; return;
} }

View file

@ -3,6 +3,7 @@
namespace Tests\Wallabag\ApiBundle\Controller; namespace Tests\Wallabag\ApiBundle\Controller;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\ApiBundle\Entity\Client;
class DeveloperControllerTest extends WallabagCoreTestCase class DeveloperControllerTest extends WallabagCoreTestCase
{ {
@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$this->assertContains('My app', $alert[0]); $this->assertContains('My app', $alert[0]);
} }
/**
* @depends testCreateClient
*/
public function testCreateToken() public function testCreateToken()
{ {
$client = $this->getClient(); $client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager'); $apiClient = $this->createApiClientForUser('admin');
$apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
$client->request('POST', '/oauth/v2/token', [ $client->request('POST', '/oauth/v2/token', [
'grant_type' => 'password', 'grant_type' => 'password',
@ -83,6 +80,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
public function testRemoveClient() public function testRemoveClient()
{ {
$client = $this->getClient(); $client = $this->getClient();
$adminApiClient = $this->createApiClientForUser('admin');
$em = $client->getContainer()->get('doctrine.orm.entity_manager'); $em = $client->getContainer()->get('doctrine.orm.entity_manager');
// Try to remove an admin's client with a wrong user // Try to remove an admin's client with a wrong user
@ -90,12 +88,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$client->request('GET', '/developer'); $client->request('GET', '/developer');
$this->assertContains('no_client', $client->getResponse()->getContent()); $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'); $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()); $this->assertEquals(403, $client->getResponse()->getStatusCode());
// Try to remove the admin's client with the good user // Try to remove the admin's client with the good user
@ -111,7 +105,29 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$client->click($link); $client->click($link);
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId()); $this->assertNull(
$this->assertGreaterThan(count($newNbClients), count($nbClients)); $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;
} }
} }

View file

@ -3,8 +3,10 @@
namespace Tests\Wallabag\ApiBundle\Controller; namespace Tests\Wallabag\ApiBundle\Controller;
use Tests\Wallabag\ApiBundle\WallabagApiTestCase; use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Helper\ContentProxy; use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\UserBundle\Entity\User;
class EntryRestControllerTest extends WallabagApiTestCase class EntryRestControllerTest extends WallabagApiTestCase
{ {
@ -801,22 +803,28 @@ class EntryRestControllerTest extends WallabagApiTestCase
public function testDeleteEntriesTagsListAction() public function testDeleteEntriesTagsListAction()
{ {
$entry = $this->client->getContainer()->get('doctrine.orm.entity_manager') $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
->getRepository('WallabagCoreBundle:Entry') $entry = new Entry($em->getReference(User::class, 1));
->findByUrlAndUserId('http://0.0.0.0/entry4', 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(); $em->clear();
$this->assertCount(4, $tags);
$list = [ $list = [
[ [
'url' => 'http://0.0.0.0/entry4', 'url' => 'http://0.0.0.0/test-entry',
'tags' => 'new tag 1, new tag 2', 'tags' => 'foo-tag, bar-tag',
], ],
]; ];
$this->client->request('DELETE', '/api/entries/tags/list?list='.json_encode($list)); $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() public function testPostEntriesListAction()
@ -841,9 +849,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
public function testDeleteEntriesListAction() 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 = [ $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/test-entry1',
'http://0.0.0.0/entry3', 'http://0.0.0.0/test-entry-not-exist',
]; ];
$this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list)); $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); $content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertTrue($content[0]['entry']); $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->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() public function testLimitBulkAction()

View file

@ -22,36 +22,35 @@ class TagRestControllerTest extends WallabagApiTestCase
return end($content); return end($content);
} }
/** public function testDeleteUserTag()
* @depends testGetUserTags
*/
public function testDeleteUserTag($tag)
{ {
$tagName = $tag['label']; $tagLabel = 'tagtest';
$tag = new Tag();
$tag->setLabel($tagLabel);
$this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json'); $em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$em->persist($tag);
$em->flush();
$em->clear();
$this->client->request('DELETE', '/api/tags/'.$tag->getId().'.json');
$this->assertEquals(200, $this->client->getResponse()->getStatusCode()); $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
$content = json_decode($this->client->getResponse()->getContent(), true); $content = json_decode($this->client->getResponse()->getContent(), true);
$this->assertArrayHasKey('label', $content); $this->assertArrayHasKey('label', $content);
$this->assertEquals($tag['label'], $content['label']); $this->assertEquals($tag->getLabel(), $content['label']);
$this->assertEquals($tag['slug'], $content['slug']); $this->assertEquals($tag->getSlug(), $content['slug']);
$entries = $this->client->getContainer() $entries = $em->getRepository('WallabagCoreBundle:Entry')
->get('doctrine.orm.entity_manager') ->findAllByTagId($this->user->getId(), $tag->getId());
->getRepository('WallabagCoreBundle:Entry')
->findAllByTagId($this->user->getId(), $tag['id']);
$this->assertCount(0, $entries); $this->assertCount(0, $entries);
$tag = $this->client->getContainer() $tag = $em->getRepository('WallabagCoreBundle:Tag')->findOneByLabel($tagLabel);
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Tag')
->findOneByLabel($tagName);
$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() public function dataForDeletingTagByLabel()

View file

@ -8,7 +8,7 @@ use Symfony\Component\BrowserKit\Cookie;
abstract class WallabagApiTestCase extends WebTestCase abstract class WallabagApiTestCase extends WebTestCase
{ {
/** /**
* @var Client * @var \Symfony\Bundle\FrameworkBundle\Client
*/ */
protected $client = null; protected $client = null;
@ -23,7 +23,7 @@ abstract class WallabagApiTestCase extends WebTestCase
} }
/** /**
* @return Client * @return \Symfony\Bundle\FrameworkBundle\Client
*/ */
protected function createAuthorizedClient() protected function createAuthorizedClient()
{ {

View file

@ -55,7 +55,7 @@ class ExportCommandTest extends WallabagCoreTestCase
'username' => 'admin', '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'); $this->assertFileExists('admin-export.json');
} }

View file

@ -2,8 +2,11 @@
namespace Tests\Wallabag\CoreBundle\Command; namespace Tests\Wallabag\CoreBundle\Command;
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand; use Doctrine\Bundle\DoctrineBundle\Command\CreateDatabaseDoctrineCommand;
use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand; use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArrayInput; use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput; use Symfony\Component\Console\Output\NullOutput;
@ -18,7 +21,9 @@ class InstallCommandTest extends WallabagCoreTestCase
{ {
parent::setUp(); 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" * LOG: statement: CREATE DATABASE "wallabag"
* ERROR: source database "template1" is being accessed by other users * 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.'); $this->markTestSkipped('PostgreSQL spotted: can\'t find a good way to drop current database, skipping.');
} }
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();
}
// disable doctrine-test-bundle
StaticDriver::setKeepStaticConnections(false);
$this->resetDatabase($this->getClient());
} }
/** public function tearDown()
* Ensure next tests will have a clean database.
*/
public static function tearDownAfterClass()
{ {
$application = new Application(static::$kernel); $databasePath = getenv('TEST_DATABASE_PATH');
$application->setAutoExit(false); // 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);
}
$application->run(new ArrayInput([ // enable doctrine-test-bundle
'command' => 'doctrine:schema:drop', StaticDriver::setKeepStaticConnections(true);
'--no-interaction' => true, parent::tearDown();
'--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());
} }
public function testRunInstallCommand() public function testRunInstallCommand()
@ -120,7 +135,7 @@ class InstallCommandTest extends WallabagCoreTestCase
{ {
// skipped SQLite check when database is removed because while testing for the connection, // 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 // 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.'); $this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
} }

View file

@ -67,8 +67,17 @@ class ConfigControllerTest extends WallabagCoreTestCase
public function testChangeReadingSpeed() public function testChangeReadingSpeed()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $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'); $crawler = $client->request('GET', '/unread/list');
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
$dataFilters = [ $dataFilters = [
@ -409,6 +418,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
public function testTaggingRuleCreation() public function testTaggingRuleCreation()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/config'); $crawler = $client->request('GET', '/config');
@ -939,6 +949,7 @@ class ConfigControllerTest extends WallabagCoreTestCase
public function testSwitchViewMode() public function testSwitchViewMode()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$client->request('GET', '/unread/list'); $client->request('GET', '/unread/list');

View file

@ -71,6 +71,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testGetNew() public function testGetNew()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/new'); $crawler = $client->request('GET', '/new');
@ -84,6 +85,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testPostNewViaBookmarklet() public function testPostNewViaBookmarklet()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/'); $crawler = $client->request('GET', '/');
@ -195,6 +197,12 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testPostNewOkUrlExist() public function testPostNewOkUrlExist()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$entry = new Entry($this->getLoggedInUser());
$entry->setUrl($this->url);
$this->getEntityManager()->persist($entry);
$this->getEntityManager()->flush();
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/new'); $crawler = $client->request('GET', '/new');
@ -288,7 +296,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$tags = $entry->getTags(); $tags = $entry->getTags();
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
$this->assertEquals('wallabag', $tags[0]->getLabel()); $this->assertContains('wallabag', $tags);
$em->remove($entry); $em->remove($entry);
$em->flush(); $em->flush();
@ -317,7 +325,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$tags = $entry->getTags(); $tags = $entry->getTags();
$this->assertCount(2, $tags); $this->assertCount(2, $tags);
$this->assertEquals('wallabag', $tags[1]->getLabel()); $this->assertContains('wallabag', $tags);
$em->remove($entry); $em->remove($entry);
$em->flush(); $em->flush();
@ -364,24 +372,23 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertEquals('/all/list', $client->getResponse()->getTargetUrl()); $this->assertEquals('/all/list', $client->getResponse()->getTargetUrl());
} }
/**
* @depends testPostNewOk
*/
public function testView() public function testView()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl('http://example.com/foo');
->getRepository('WallabagCoreBundle:Entry') $entry->setTitle('title foo');
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $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->assertEquals(200, $client->getResponse()->getStatusCode());
$this->assertGreaterThan(1, $body = $crawler->filter('body')->extract(['_text'])); $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'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$em = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager'); $entry->setUrl($this->url);
$entry->setTitle('title foo');
$entry->setContent('');
$this->getEntityManager()->persist($entry);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
$content = $em $client->request('GET', '/reload/'.$entry->getId());
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
// empty content
$content->setContent('');
$em->persist($content);
$em->flush();
$client->request('GET', '/reload/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$content = $em $entry = $this->getEntityManager()
->getRepository('WallabagCoreBundle:Entry') ->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'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$em = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager'); $entry->setUrl('http://0.0.0.0/failed.html');
$this->getEntityManager()->persist($entry);
$this->getEntityManager()->flush();
$content = $em $client->request('GET', '/reload/'.$entry->getId());
->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();
$client->request('GET', '/reload/'.$content->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
// force EntityManager to clear previous entity // force EntityManager to clear previous entity
// otherwise, retrieve the same entity will retrieve change from the previous request :0 // otherwise, retrieve the same entity will retrieve change from the previous request :0
$em->clear(); $this->getEntityManager()->clear();
$newContent = $em $newContent = $this->getEntityManager()
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->find($content->getId()); ->find($entry->getId());
$newContent->setUrl($this->url);
$em->persist($newContent);
$em->flush();
$this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent()); $this->assertNotEquals($client->getContainer()->getParameter('wallabag_core.fetching_error_message'), $newContent->getContent());
} }
@ -460,12 +452,12 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$crawler = $client->request('GET', '/edit/'.$content->getId()); $crawler = $client->request('GET', '/edit/'.$entry->getId());
$this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(200, $client->getResponse()->getStatusCode());
@ -478,12 +470,12 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$crawler = $client->request('GET', '/edit/'.$content->getId()); $crawler = $client->request('GET', '/edit/'.$entry->getId());
$this->assertEquals(200, $client->getResponse()->getStatusCode()); $this->assertEquals(200, $client->getResponse()->getStatusCode());
@ -508,19 +500,20 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$this->getEntityManager()->clear();
$client->request('GET', '/archive/'.$content->getId()); $client->request('GET', '/archive/'.$entry->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$res = $client->getContainer() $res = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->find($content->getId()); ->find($entry->getId());
$this->assertEquals($res->isArchived(), true); $this->assertEquals($res->isArchived(), true);
} }
@ -530,19 +523,20 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$this->getEntityManager()->clear();
$client->request('GET', '/star/'.$content->getId()); $client->request('GET', '/star/'.$entry->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$res = $client->getContainer() $res = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
->findOneById($content->getId()); ->findOneById($entry->getId());
$this->assertEquals($res->isStarred(), true); $this->assertEquals($res->isStarred(), true);
} }
@ -552,16 +546,16 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$client->request('GET', '/delete/'.$content->getId()); $client->request('GET', '/delete/'.$entry->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$client->request('GET', '/delete/'.$content->getId()); $client->request('GET', '/delete/'.$entry->getId());
$this->assertEquals(404, $client->getResponse()->getStatusCode()); $this->assertEquals(404, $client->getResponse()->getStatusCode());
} }
@ -627,7 +621,13 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testFilterOnReadingTime() public function testFilterOnReadingTime()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $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'); $crawler = $client->request('GET', '/unread/list');
@ -666,9 +666,20 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testFilterOnReadingTimeOnlyUpper() public function testFilterOnReadingTimeOnlyUpper()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $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(); $form = $crawler->filter('button[id=submit-filter]')->form();
@ -678,12 +689,13 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(3, $crawler->filter('div[class=entry]')); $this->assertCount(5, $crawler->filter('div[class=entry]'));
} }
public function testFilterOnReadingTimeOnlyLower() public function testFilterOnReadingTimeOnlyLower()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -696,12 +708,22 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $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() public function testFilterOnUnreadStatus()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/all/list'); $crawler = $client->request('GET', '/all/list');
@ -714,12 +736,23 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $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]')); $this->assertCount(5, $crawler->filter('div[class=entry]'));
} }
public function testFilterOnCreationDate() public function testFilterOnCreationDate()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -733,7 +766,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(6, $crawler->filter('div[class=entry]')); $this->assertCount(5, $crawler->filter('div[class=entry]'));
$data = [ $data = [
'entry_filter[createdAt][left_date]' => date('d/m/Y'), 'entry_filter[createdAt][left_date]' => date('d/m/Y'),
@ -742,7 +775,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(6, $crawler->filter('div[class=entry]')); $this->assertCount(5, $crawler->filter('div[class=entry]'));
$data = [ $data = [
'entry_filter[createdAt][left_date]' => '01/01/1970', 'entry_filter[createdAt][left_date]' => '01/01/1970',
@ -786,6 +819,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testFilterOnDomainName() public function testFilterOnDomainName()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -818,6 +852,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testFilterOnStatus() public function testFilterOnStatus()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -839,6 +874,7 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testPreviewPictureFilter() public function testPreviewPictureFilter()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -846,14 +882,21 @@ class EntryControllerTest extends WallabagCoreTestCase
$form['entry_filter[previewPicture]']->tick(); $form['entry_filter[previewPicture]']->tick();
$crawler = $client->submit($form); $crawler = $client->submit($form);
$this->assertCount(2, $crawler->filter('div[class=entry]')); $this->assertCount(1, $crawler->filter('div[class=entry]'));
} }
public function testFilterOnLanguage() public function testFilterOnLanguage()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $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'); $crawler = $client->request('GET', '/unread/list');
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
$data = [ $data = [
@ -877,10 +920,14 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$content = $client->getContainer() // sharing is enabled
->get('doctrine.orm.entity_manager') $client->getContainer()->get('craue_config')->set('share_public', 1);
->getRepository('WallabagCoreBundle:Entry')
->findOneByUser($this->getLoggedInUserId()); $content = new Entry($this->getLoggedInUser());
$content->setUrl($this->url);
$this->getEntityManager()->persist($content);
$this->getEntityManager()->flush();
$this->getEntityManager()->clear();
// no uid // no uid
$client->request('GET', '/share/'.$content->getUid()); $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'; $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); $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() $content = $client->getContainer()
->get('doctrine.orm.entity_manager') ->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry') ->getRepository('WallabagCoreBundle:Entry')
@ -987,28 +1048,19 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('empty'); $this->logInAs('empty');
$client = $this->getClient(); $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 // Redirect to homepage
$config = $user->getConfig(); $config = $this->getLoggedInUser()->getConfig();
$config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE); $config->setActionMarkAsRead(Config::REDIRECT_TO_HOMEPAGE);
$em->persist($config); $this->getEntityManager()->persist($config);
$em->flush();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/view/'.$content->getId()); $this->getEntityManager()->flush();
$client->request('GET', '/archive/'.$content->getId());
$client->request('GET', '/view/'.$entry->getId());
$client->request('GET', '/archive/'.$entry->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertEquals('/', $client->getResponse()->headers->get('location')); $this->assertEquals('/', $client->getResponse()->headers->get('location'));
@ -1019,46 +1071,36 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->logInAs('empty'); $this->logInAs('empty');
$client = $this->getClient(); $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 // Redirect to current page
$config = $user->getConfig(); $config = $this->getLoggedInUser()->getConfig();
$config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE); $config->setActionMarkAsRead(Config::REDIRECT_TO_CURRENT_PAGE);
$em->persist($config); $this->getEntityManager()->persist($config);
$em->flush();
$content = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl($this->url);
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId($this->url, $this->getLoggedInUserId());
$client->request('GET', '/view/'.$content->getId()); $this->getEntityManager()->flush();
$client->request('GET', '/archive/'.$content->getId());
$client->request('GET', '/view/'.$entry->getId());
$client->request('GET', '/archive/'.$entry->getId());
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $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() public function testFilterOnHttpStatus()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $client = $this->getClient();
$crawler = $client->request('GET', '/new'); $entry = new Entry($this->getLoggedInUser());
$form = $crawler->filter('form[name=entry]')->form(); $entry->setUrl('http://www.lemonde.fr/incorrect-url/');
$entry->setHttpStatus(404);
$this->getEntityManager()->persist($entry);
$data = [ $this->getEntityManager()->flush();
'entry[url]' => 'http://www.lemonde.fr/incorrect-url/',
];
$client->submit($form, $data);
$crawler = $client->request('GET', '/all/list'); $crawler = $client->request('GET', '/all/list');
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
@ -1071,14 +1113,17 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertCount(1, $crawler->filter('div[class=entry]')); $this->assertCount(1, $crawler->filter('div[class=entry]'));
$crawler = $client->request('GET', '/new'); $entry = new Entry($this->getLoggedInUser());
$form = $crawler->filter('form[name=entry]')->form(); $entry->setUrl($this->url);
$entry->setHttpStatus(200);
$this->getEntityManager()->persist($entry);
$data = [ $entry = new Entry($this->getLoggedInUser());
'entry[url]' => 'http://www.nextinpact.com/news/101235-wallabag-alternative-libre-a-pocket-creuse-petit-a-petit-son-nid.htm', $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'); $crawler = $client->request('GET', '/all/list');
$form = $crawler->filter('button[id=submit-filter]')->form(); $form = $crawler->filter('button[id=submit-filter]')->form();
@ -1106,8 +1151,15 @@ class EntryControllerTest extends WallabagCoreTestCase
public function testSearch() public function testSearch()
{ {
$this->logInAs('admin'); $this->logInAs('admin');
$this->useTheme('baggy');
$client = $this->getClient(); $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 // Search on unread list
$crawler = $client->request('GET', '/unread/list'); $crawler = $client->request('GET', '/unread/list');
@ -1118,35 +1170,37 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $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 // Search on starred list
$crawler = $client->request('GET', '/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(); $form = $crawler->filter('form[name=search]')->form();
$data = [ $data = [
'search_entry[term]' => 'title', 'search_entry[term]' => 'testeur',
]; ];
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(1, $crawler->filter('div[class=entry]')); $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'); $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(); $form = $crawler->filter('form[name=search]')->form();
$data = [ $data = [
'search_entry[term]' => 'manège', 'search_entry[term]' => 'manège',
@ -1155,7 +1209,7 @@ class EntryControllerTest extends WallabagCoreTestCase
$crawler = $client->submit($form, $data); $crawler = $client->submit($form, $data);
$this->assertCount(1, $crawler->filter('div[class=entry]')); $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 // test on list of all articles
$crawler = $client->request('GET', '/all/list'); $crawler = $client->request('GET', '/all/list');
@ -1170,6 +1224,13 @@ class EntryControllerTest extends WallabagCoreTestCase
$this->assertCount(0, $crawler->filter('div[class=entry]')); $this->assertCount(0, $crawler->filter('div[class=entry]'));
// test url search on list of all articles // 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'); $crawler = $client->request('GET', '/all/list');
$form = $crawler->filter('form[name=search]')->form(); $form = $crawler->filter('form[name=search]')->form();

View file

@ -239,7 +239,7 @@ class ExportControllerTest extends WallabagCoreTestCase
$this->assertEquals($contentInDB->getLanguage(), $content[0]['language']); $this->assertEquals($contentInDB->getLanguage(), $content[0]['language']);
$this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']); $this->assertEquals($contentInDB->getReadingtime(), $content[0]['reading_time']);
$this->assertEquals($contentInDB->getDomainname(), $content[0]['domain_name']); $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() public function testXmlExport()

View file

@ -3,6 +3,7 @@
namespace Tests\Wallabag\CoreBundle\Controller; namespace Tests\Wallabag\CoreBundle\Controller;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag; use Wallabag\CoreBundle\Entity\Tag;
class TagControllerTest extends WallabagCoreTestCase class TagControllerTest extends WallabagCoreTestCase
@ -24,10 +25,11 @@ class TagControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$entry = $client->getContainer() $entry = new Entry($this->getLoggedInUser());
->get('doctrine.orm.entity_manager') $entry->setUrl('http://0.0.0.0/foo');
->getRepository('WallabagCoreBundle:Entry') $this->getEntityManager()->persist($entry);
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); $this->getEntityManager()->flush();
$this->getEntityManager()->clear();
$crawler = $client->request('GET', '/view/'.$entry->getId()); $crawler = $client->request('GET', '/view/'.$entry->getId());
@ -41,23 +43,15 @@ class TagControllerTest extends WallabagCoreTestCase
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
// be sure to reload the entry // be sure to reload the entry
$entry = $client->getContainer() $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
->get('doctrine.orm.entity_manager') $this->assertCount(1, $entry->getTags());
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
$this->assertEquals(4, count($entry->getTags()));
// tag already exists and already assigned // tag already exists and already assigned
$client->submit($form, $data); $client->submit($form, $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$newEntry = $client->getContainer() $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
->get('doctrine.orm.entity_manager') $this->assertCount(1, $entry->getTags());
->getRepository('WallabagCoreBundle:Entry')
->find($entry->getId());
$this->assertEquals(4, count($newEntry->getTags()));
// tag already exists but still not assigned to this entry // tag already exists but still not assigned to this entry
$data = [ $data = [
@ -67,12 +61,8 @@ class TagControllerTest extends WallabagCoreTestCase
$client->submit($form, $data); $client->submit($form, $data);
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$newEntry = $client->getContainer() $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
->get('doctrine.orm.entity_manager') $this->assertCount(2, $entry->getTags());
->getRepository('WallabagCoreBundle:Entry')
->find($entry->getId());
$this->assertEquals(4, count($newEntry->getTags()));
} }
public function testAddMultipleTagToEntry() public function testAddMultipleTagToEntry()
@ -116,15 +106,14 @@ class TagControllerTest extends WallabagCoreTestCase
$this->logInAs('admin'); $this->logInAs('admin');
$client = $this->getClient(); $client = $this->getClient();
$entry = $client->getContainer() $tag = new Tag();
->get('doctrine.orm.entity_manager') $tag->setLabel($this->tagName);
->getRepository('WallabagCoreBundle:Entry') $entry = new Entry($this->getLoggedInUser());
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId()); $entry->setUrl('http://0.0.0.0/foo');
$entry->addTag($tag);
$tag = $client->getContainer() $this->getEntityManager()->persist($entry);
->get('doctrine.orm.entity_manager') $this->getEntityManager()->flush();
->getRepository('WallabagCoreBundle:Tag') $this->getEntityManager()->clear();
->findOneByEntryAndTagLabel($entry, $this->tagName);
// We make a first request to set an history and test redirection after tag deletion // We make a first request to set an history and test redirection after tag deletion
$client->request('GET', '/view/'.$entry->getId()); $client->request('GET', '/view/'.$entry->getId());
@ -134,12 +123,8 @@ class TagControllerTest extends WallabagCoreTestCase
$this->assertEquals(302, $client->getResponse()->getStatusCode()); $this->assertEquals(302, $client->getResponse()->getStatusCode());
$this->assertEquals($entryUri, $client->getResponse()->getTargetUrl()); $this->assertEquals($entryUri, $client->getResponse()->getTargetUrl());
// re-retrieve the entry to be sure to get fresh data from database (mostly for tags) // re-retrieve the entry to be sure to get fresh data from database (mostly for tags)
$entry = $client->getContainer() $entry = $this->getEntityManager()->getRepository(Entry::class)->find($entry->getId());
->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId('http://0.0.0.0/entry1', $this->getLoggedInUserId());
$this->assertNotContains($this->tagName, $entry->getTags()); $this->assertNotContains($this->tagName, $entry->getTags());
$client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId()); $client->request('GET', '/remove-tag/'.$entry->getId().'/'.$tag->getId());

View file

@ -2,11 +2,20 @@
namespace Tests\Wallabag\CoreBundle; namespace Tests\Wallabag\CoreBundle;
use Symfony\Bundle\FrameworkBundle\Client;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\BrowserKit\Cookie; 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 abstract class WallabagCoreTestCase extends WebTestCase
{ {
/**
* @var Client|null
*/
private $client = null; private $client = null;
public function getClient() public function getClient()
@ -21,6 +30,44 @@ abstract class WallabagCoreTestCase extends WebTestCase
$this->client = static::createClient(); $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. * Login a user without making a HTTP request.
* If we make a HTTP request we lose ability to mock service in the container. * 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'); $firewallName = $container->getParameter('fos_user.firewall_name');
$user = $userManager->findUserBy(array('username' => $username)); $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->set('_security_'.$firewallName, serialize($container->get('security.token_storage')->getToken()));
$session->save(); $session->save();
@ -64,6 +111,23 @@ abstract class WallabagCoreTestCase extends WebTestCase
$this->client->submit($form, $data); $this->client->submit($form, $data);
} }
/**
* Return the user of the logged in user.
* You should be sure that you called `logInAs` before.
*
* @return User
*/
public function getLoggedInUser()
{
$token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
if (null !== $token) {
return $token->getUser();
}
throw new \RuntimeException('No logged in User.');
}
/** /**
* Return the user id of the logged in user. * Return the user id of the logged in user.
* You should be sure that you called `logInAs` before. * You should be sure that you called `logInAs` before.
@ -72,13 +136,15 @@ abstract class WallabagCoreTestCase extends WebTestCase
*/ */
public function getLoggedInUserId() public function getLoggedInUserId()
{ {
$token = static::$kernel->getContainer()->get('security.token_storage')->getToken(); return $this->getLoggedInUser()->getId();
}
if (null !== $token) { public function useTheme($theme)
return $token->getUser()->getId(); {
} $config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser());
$config->setTheme($theme);
throw new \RuntimeException('No logged in User.'); $this->getEntityManager()->persist($config);
$this->getEntityManager()->flush();
} }
/** /**

View file

@ -95,6 +95,7 @@ class ImportCommandTest extends WallabagCoreTestCase
'username' => 1, 'username' => 1,
'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json', 'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.root_dir').'/../tests/Wallabag/ImportBundle/fixtures/wallabag-v2-read.json',
'--useUserId' => true, '--useUserId' => true,
'--importer' => 'v2',
]); ]);
} }
} }

View file

@ -119,9 +119,10 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase
$this->getLoggedInUserId() $this->getLoggedInUserId()
); );
$this->assertNotEmpty($content->getMimetype(), 'Mimetype for http://www.liberation.fr is ok'); // empty because it wasn't re-imported
$this->assertNotEmpty($content->getPreviewPicture(), 'Preview picture for http://www.liberation.fr is ok'); $this->assertEmpty($content->getMimetype(), 'Mimetype for http://www.liberation.fr is empty');
$this->assertNotEmpty($content->getLanguage(), 'Language for http://www.liberation.fr is ok'); $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(); $tags = $content->getTags();
$this->assertContains('foot', $tags, 'It includes the "foot" tag'); $this->assertContains('foot', $tags, 'It includes the "foot" tag');