Move test files directly under tests/ directory

This commit is contained in:
Yassine Guedidi
2024-02-19 00:45:58 +01:00
parent a37b385c23
commit 24da70e338
117 changed files with 4 additions and 4 deletions

View File

@ -0,0 +1,100 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
class CleanDuplicatesCommandTest extends WallabagCoreTestCase
{
public function testRunCleanDuplicates()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:clean-duplicates');
$tester = new CommandTester($command);
$tester->execute([]);
$this->assertStringContainsString('Cleaning through 3 user accounts', $tester->getDisplay());
$this->assertStringContainsString('Finished cleaning. 0 duplicates found in total', $tester->getDisplay());
}
public function testRunCleanDuplicatesCommandWithBadUsername()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:clean-duplicates');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'unknown',
]);
$this->assertStringContainsString('User "unknown" not found', $tester->getDisplay());
}
public function testRunCleanDuplicatesCommandForUser()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:clean-duplicates');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Cleaned 0 duplicates for user admin', $tester->getDisplay());
}
public function testDuplicate()
{
$url = 'https://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
$client = $this->getTestClient();
$em = $client->getContainer()->get(EntityManagerInterface::class);
$this->logInAs('admin');
$nbEntries = $em->getRepository(Entry::class)->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
$this->assertCount(0, $nbEntries);
$user = $em->getRepository(User::class)->findOneById($this->getLoggedInUserId());
$entry1 = new Entry($user);
$entry1->setUrl($url);
$entry2 = new Entry($user);
$entry2->setUrl($url);
$em->persist($entry1);
$em->persist($entry2);
$em->flush();
$nbEntries = $em->getRepository(Entry::class)->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
$this->assertCount(2, $nbEntries);
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:clean-duplicates');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Cleaned 1 duplicates for user admin', $tester->getDisplay());
$nbEntries = $em->getRepository(Entry::class)->findAllByUrlAndUserId($url, $this->getLoggedInUserId());
$this->assertCount(1, $nbEntries);
$query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
$query->setParameter('url', $url);
$query->execute();
}
}

View File

@ -0,0 +1,69 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class ExportCommandTest extends WallabagCoreTestCase
{
public function testExportCommandWithoutUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username")');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:export');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testExportCommandWithBadUsername()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:export');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'unknown',
]);
$this->assertStringContainsString('User "unknown" not found', $tester->getDisplay());
}
public function testExportCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:export');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Exporting 5 entrie(s) for user admin...', $tester->getDisplay());
$this->assertStringContainsString('Done', $tester->getDisplay());
$this->assertFileExists('admin-export.json');
}
public function testExportCommandWithSpecialPath()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:export');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
'filepath' => 'specialexport.json',
]);
$this->assertFileExists('specialexport.json');
}
}

View File

@ -0,0 +1,90 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\User;
class GenerateUrlHashesCommandTest extends WallabagCoreTestCase
{
public function testRunGenerateUrlHashesCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([]);
$this->assertStringContainsString('Generating hashed urls for "3" users', $tester->getDisplay());
$this->assertStringContainsString('Finished generated hashed urls', $tester->getDisplay());
}
public function testRunGenerateUrlHashesCommandWithBadUsername()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'unknown',
]);
$this->assertStringContainsString('User "unknown" not found', $tester->getDisplay());
}
public function testRunGenerateUrlHashesCommandForUser()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Generated hashed urls for user: admin', $tester->getDisplay());
}
public function testGenerateUrls()
{
$url = 'http://www.lemonde.fr/sport/visuel/2017/05/05/rondelle-prison-blanchissage-comprendre-le-hockey-sur-glace_5122587_3242.html';
$client = $this->getTestClient();
$em = $client->getContainer()->get(EntityManagerInterface::class);
$this->logInAs('admin');
$user = $em->getRepository(User::class)->findOneById($this->getLoggedInUserId());
$entry1 = new Entry($user);
$entry1->setUrl($url);
$em->persist($entry1);
$em->flush();
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:generate-hashed-urls');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Generated hashed urls for user: admin', $tester->getDisplay());
$entry = $em->getRepository(Entry::class)->findOneByUrl($url);
$this->assertSame($entry->getHashedUrl(), hash('sha1', $url));
$query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
$query->setParameter('url', $url);
$query->execute();
}
}

View File

@ -0,0 +1,94 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command\Import;
use Doctrine\ORM\NoResultException;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class ImportCommandTest extends WallabagCoreTestCase
{
public function testRunImportCommandWithoutArguments()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testRunImportCommandWithoutFilepath()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('not found');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
'filepath' => 1,
]);
}
public function testRunImportCommandWithWrongUsername()
{
$this->expectException(NoResultException::class);
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'random',
'filepath' => './',
]);
}
public function testRunImportCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.project_dir') . '/tests/fixtures/Import/wallabag-v2-read.json',
'--importer' => 'v2',
]);
$this->assertStringContainsString('imported', $tester->getDisplay());
$this->assertStringContainsString('already saved', $tester->getDisplay());
}
public function testRunImportCommandWithUserId()
{
$this->logInAs('admin');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import');
$tester = new CommandTester($command);
$tester->execute([
'username' => $this->getLoggedInUserId(),
'filepath' => $application->getKernel()->getContainer()->getParameter('kernel.project_dir') . '/tests/fixtures/Import/wallabag-v2-read.json',
'--useUserId' => true,
'--importer' => 'v2',
]);
$this->assertStringContainsString('imported', $tester->getDisplay());
$this->assertStringContainsString('already saved', $tester->getDisplay());
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command\Import;
use M6Web\Component\RedisMock\RedisMockFactory;
use Predis\Client;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class RedisWorkerCommandTest extends WallabagCoreTestCase
{
public function testRunRedisWorkerCommandWithoutArguments()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "serviceName")');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import:redis-worker');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testRunRedisWorkerCommandWithBadService()
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('No queue or consumer found for service name');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:import:redis-worker');
$tester = new CommandTester($command);
$tester->execute([
'serviceName' => 'YOMONSERVICE',
]);
}
public function testRunRedisWorkerCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$factory = new RedisMockFactory();
$redisMock = $factory->getAdapter(Client::class, true);
$application->getKernel()->getContainer()->set(Client::class, $redisMock);
// put a fake message in the queue so the worker will stop after reading that message
// instead of waiting for others
$redisMock->lpush('wallabag.import.readability', '{}');
$command = $application->find('wallabag:import:redis-worker');
$tester = new CommandTester($command);
$tester->execute([
'serviceName' => 'readability',
'--maxIterations' => 1,
]);
$this->assertStringContainsString('Worker started at', $tester->getDisplay());
$this->assertStringContainsString('Waiting for message', $tester->getDisplay());
}
}

View File

@ -0,0 +1,287 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Command\LazyCommand;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Command\InstallCommand;
class InstallCommandTest extends WallabagCoreTestCase
{
public static function setUpBeforeClass(): void
{
// disable doctrine-test-bundle
StaticDriver::setKeepStaticConnections(false);
}
public static function tearDownAfterClass(): void
{
// enable doctrine-test-bundle
StaticDriver::setKeepStaticConnections(true);
}
protected function setUp(): void
{
parent::setUp();
/** @var Connection $connection */
$connection = $this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection();
if ($connection->getDatabasePlatform() instanceof PostgreSQLPlatform) {
/*
* LOG: statement: CREATE DATABASE "wallabag"
* ERROR: source database "template1" is being accessed by other users
* DETAIL: There is 1 other session using the database.
* STATEMENT: CREATE DATABASE "wallabag"
* FATAL: database "wallabag" does not exist
*
* http://stackoverflow.com/a/14374832/569101
*/
$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("DATABASE_URL=sqlite:///$databasePath?charset=utf8");
// The environnement has been changed, recreate the client in order to update connection
parent::setUp();
}
$this->resetDatabase($this->getTestClient());
}
protected function tearDown(): void
{
$databaseUrl = getenv('DATABASE_URL');
$databasePath = parse_url($databaseUrl, \PHP_URL_PATH);
// Remove the real environnement variable
putenv('DATABASE_URL');
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 = $this->getNewClient();
$this->resetDatabase($client);
}
parent::tearDown();
}
public function testRunInstallCommand()
{
$command = $this->getCommand();
// enable calling other commands for MySQL only because rollback isn't supported
if (!$this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$command->disableRunOtherCommands();
}
$tester = new CommandTester($command);
$tester->setInputs([
'y', // dropping database
'y', // create super admin
'username_' . uniqid('', true), // username
'password_' . uniqid('', true), // password
'email_' . uniqid('', true) . '@wallabag.it', // email
]);
$tester->execute([]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
}
public function testRunInstallCommandWithReset()
{
if ($this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->markTestSkipped('Rollback are not properly handled for MySQL, skipping.');
}
$command = $this->getCommand();
$command->disableRunOtherCommands();
$tester = new CommandTester($command);
$tester->setInputs([
'y', // create super admin
'username_' . uniqid('', true), // username
'password_' . uniqid('', true), // password
'email_' . uniqid('', true) . '@wallabag.it', // email
]);
$tester->execute([
'--reset' => true,
]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
// we force to reset everything
$this->assertStringContainsString('Dropping database, creating database and schema, clearing the cache', $tester->getDisplay());
}
public function testRunInstallCommandWithDatabaseRemoved()
{
if ($this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->markTestSkipped('Rollback are not properly handled for MySQL, skipping.');
}
// 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->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
$this->markTestSkipped('SQLite spotted: can\'t test with database removed.');
}
$application = new Application($this->getTestClient()->getKernel());
// drop database first, so the install command won't ask to reset things
$command = $application->find('doctrine:database:drop');
$command->run(new ArrayInput([
'--force' => true,
]), new NullOutput());
// start a new application to avoid lagging connexion to pgsql
$this->getNewClient();
$command = $this->getCommand();
$tester = new CommandTester($command);
$tester->setInputs([
'y', // create super admin
'username_' . uniqid('', true), // username
'password_' . uniqid('', true), // password
'email_' . uniqid('', true) . '@wallabag.it', // email
]);
$tester->execute([]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
// the current database doesn't already exist
$this->assertStringContainsString('Creating database and schema, clearing the cache', $tester->getDisplay());
}
public function testRunInstallCommandChooseResetSchema()
{
if ($this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->markTestSkipped('Rollback are not properly handled for MySQL, skipping.');
}
$command = $this->getCommand();
$command->disableRunOtherCommands();
$tester = new CommandTester($command);
$tester->setInputs([
'n', // don't want to reset the entire database
'y', // do want to reset the schema
'n', // don't want to create a new user
]);
$tester->execute([]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
$this->assertStringContainsString('Dropping schema and creating schema', $tester->getDisplay());
}
public function testRunInstallCommandChooseNothing()
{
/*
* [PHPUnit\Framework\Error\Warning (2)]
* filemtime(): stat failed for /home/runner/work/wallabag/wallabag/var/cache/tes_/ContainerNVNxA24/appAppKernelTestDebugContainer.php
*
* I don't know from where the "/tes_/" come from, it should be "/test/" instead ...
*/
if ($this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->markTestSkipped('That test is failing when using MySQL when clearing the cache (see code comment)');
}
$application = new Application($this->getTestClient()->getKernel());
// drop database first, so the install command won't ask to reset things
$command = $application->find('doctrine:database:drop');
$command->run(new ArrayInput([
'--force' => true,
]), new NullOutput());
$this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->close();
$command = $application->find('doctrine:database:create');
$command->run(new ArrayInput([]), new NullOutput());
$command = $this->getCommand();
$tester = new CommandTester($command);
$tester->setInputs([
'n', // don't want to reset the entire database
'n', // don't want to create a new user
]);
$tester->execute([]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
$this->assertStringContainsString('Creating schema', $tester->getDisplay());
}
public function testRunInstallCommandNoInteraction()
{
if ($this->getTestClient()->getContainer()->get(ManagerRegistry::class)->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
$this->markTestSkipped('Rollback are not properly handled for MySQL, skipping.');
}
$command = $this->getCommand();
$command->disableRunOtherCommands();
$tester = new CommandTester($command);
$tester->execute([], [
'interactive' => false,
]);
$this->assertStringContainsString('Checking system requirements.', $tester->getDisplay());
$this->assertStringContainsString('Setting up database.', $tester->getDisplay());
$this->assertStringContainsString('Administration setup.', $tester->getDisplay());
$this->assertStringContainsString('Config setup.', $tester->getDisplay());
}
private function getCommand(): InstallCommand
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:install');
if ($command instanceof LazyCommand) {
$command = $command->getCommand();
}
\assert($command instanceof InstallCommand);
return $command;
}
}

View File

@ -0,0 +1,65 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class ListUserCommandTest extends WallabagCoreTestCase
{
public function testRunListUserCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([]);
$this->assertStringContainsString('3/3 user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithLimit()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'--limit' => 2,
]);
$this->assertStringContainsString('2/3 user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithSearch()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'search' => 'boss',
]);
$this->assertStringContainsString('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
}
public function testRunListUserCommandWithSearchAndLimit()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:list');
$tester = new CommandTester($command);
$tester->execute([
'search' => 'bo',
'--limit' => 1,
]);
$this->assertStringContainsString('1/3 (filtered) user(s) displayed.', $tester->getDisplay());
}
}

View File

@ -0,0 +1,157 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
class ReloadEntryCommandTest extends WallabagCoreTestCase
{
public $url = 'https://www.lemonde.fr/pixels/article/2015/03/28/plongee-dans-l-univers-d-ingress-le-jeu-de-google-aux-frontieres-du-reel_4601155_4408996.html';
/**
* @var Entry
*/
public $adminEntry;
/**
* @var Entry
*/
public $bobEntry;
/**
* @var Entry
*/
public $bobParsedEntry;
/**
* @var Entry
*/
public $bobNotParsedEntry;
protected function setUp(): void
{
parent::setUp();
$userRepository = $this->getTestClient()->getContainer()->get('wallabag_user.user_repository.test');
$user = $userRepository->findOneByUserName('admin');
$this->adminEntry = new Entry($user);
$this->adminEntry->setUrl($this->url);
$this->adminEntry->setTitle('title foo');
$this->adminEntry->setContent('');
$this->getEntityManager()->persist($this->adminEntry);
$user = $userRepository->findOneByUserName('bob');
$this->bobEntry = new Entry($user);
$this->bobEntry->setUrl($this->url);
$this->bobEntry->setTitle('title foo');
$this->bobEntry->setContent('');
$this->getEntityManager()->persist($this->bobEntry);
$this->bobParsedEntry = new Entry($user);
$this->bobParsedEntry->setUrl($this->url);
$this->bobParsedEntry->setTitle('title foo');
$this->bobParsedEntry->setContent('');
$this->getEntityManager()->persist($this->bobParsedEntry);
$this->bobNotParsedEntry = new Entry($user);
$this->bobNotParsedEntry->setUrl($this->url);
$this->bobNotParsedEntry->setTitle('title foo');
$this->bobNotParsedEntry->setContent('');
$this->bobNotParsedEntry->setNotParsed(true);
$this->getEntityManager()->persist($this->bobNotParsedEntry);
$this->getEntityManager()->flush();
}
/**
* @group NetworkCalls
*/
public function testRunReloadEntryCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:entry:reload');
$tester = new CommandTester($command);
$tester->execute([], [
'interactive' => false,
]);
$reloadedEntries = $this->getTestClient()
->getContainer()
->get('wallabag_core.entry_repository.test')
->findById([$this->adminEntry->getId(), $this->bobEntry->getId()]);
foreach ($reloadedEntries as $reloadedEntry) {
$this->assertNotEmpty($reloadedEntry->getContent());
}
$this->assertStringContainsString('Done', $tester->getDisplay());
}
/**
* @group NetworkCalls
*/
public function testRunReloadEntryWithUsernameCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:entry:reload');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
], [
'interactive' => false,
]);
$entryRepository = $this->getTestClient()->getContainer()->get('wallabag_core.entry_repository.test');
$reloadedAdminEntry = $entryRepository->find($this->adminEntry->getId());
$this->assertNotEmpty($reloadedAdminEntry->getContent());
$reloadedBobEntry = $entryRepository->find($this->bobEntry->getId());
$this->assertEmpty($reloadedBobEntry->getContent());
$this->assertStringContainsString('Done', $tester->getDisplay());
}
public function testRunReloadEntryWithNotParsedOption()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:entry:reload');
$tester = new CommandTester($command);
$tester->execute([
'--only-not-parsed' => true,
]);
$entryRepository = $this->getTestClient()->getContainer()->get('wallabag_core.entry_repository.test');
$reloadedBobParsedEntry = $entryRepository->find($this->bobParsedEntry->getId());
$this->assertEmpty($reloadedBobParsedEntry->getContent());
$reloadedBobNotParsedEntry = $entryRepository->find($this->bobNotParsedEntry->getId());
$this->assertNotEmpty($reloadedBobNotParsedEntry->getContent());
$this->assertStringContainsString('Done', $tester->getDisplay());
}
public function testRunReloadEntryWithoutEntryCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:entry:reload');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'empty',
], [
'interactive' => false,
]);
$this->assertStringContainsString('No entry to reload', $tester->getDisplay());
$this->assertStringNotContainsString('Done', $tester->getDisplay());
}
}

View File

@ -0,0 +1,85 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\User;
class ShowUserCommandTest extends WallabagCoreTestCase
{
public function testRunShowUserCommandWithoutUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:show');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testRunShowUserCommandWithBadUsername()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:show');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'unknown',
]);
$this->assertStringContainsString('User "unknown" not found', $tester->getDisplay());
}
public function testRunShowUserCommandForUser()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:show');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Username: admin', $tester->getDisplay());
$this->assertStringContainsString('Email: bigboss@wallabag.org', $tester->getDisplay());
$this->assertStringContainsString('Display name: Big boss', $tester->getDisplay());
$this->assertStringContainsString('2FA (email) activated', $tester->getDisplay());
$this->assertStringContainsString('2FA (OTP) activated', $tester->getDisplay());
}
public function testShowUser()
{
$client = $this->getTestClient();
$em = $client->getContainer()->get(EntityManagerInterface::class);
$this->logInAs('admin');
/** @var User $user */
$user = $em->getRepository(User::class)->findOneById($this->getLoggedInUserId());
$user->setName('Bug boss');
$em->persist($user);
$em->flush();
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:user:show');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Display name: Bug boss', $tester->getDisplay());
}
}

View File

@ -0,0 +1,53 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
class TagAllCommandTest extends WallabagCoreTestCase
{
public function testRunTagAllCommandWithoutUsername()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "username")');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:tag:all');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testRunTagAllCommandWithBadUsername()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:tag:all');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'unknown',
]);
$this->assertStringContainsString('User "unknown" not found', $tester->getDisplay());
}
public function testRunTagAllCommand()
{
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:tag:all');
$tester = new CommandTester($command);
$tester->execute([
'username' => 'admin',
]);
$this->assertStringContainsString('Tagging entries for user admin...', $tester->getDisplay());
$this->assertStringContainsString('Done', $tester->getDisplay());
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace Tests\Wallabag\CoreBundle\Command;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Tester\CommandTester;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\CoreBundle\Entity\Entry;
class UpdatePicturesPathCommandTest extends WallabagCoreTestCase
{
public function testRunUpdatePicturesPathCommandWithoutOldURL()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Not enough arguments (missing: "old-url")');
$application = new Application($this->getTestClient()->getKernel());
$command = $application->find('wallabag:update-pictures-path');
$tester = new CommandTester($command);
$tester->execute([]);
}
public function testRunGenerateUrlHashesCommandForUser()
{
$application = new Application($this->getTestClient()->getKernel());
$this->logInAs('admin');
$url = 'https://wallabag.org/news/20230620-new-release-wallabag-260/';
$command = $application->find('wallabag:update-pictures-path');
$client = $this->getTestClient();
$em = $client->getContainer()->get(EntityManagerInterface::class);
$entry = new Entry($this->getLoggedInUser());
$entry->setUrl($url);
$entry->setPreviewPicture('https://old-url.test/mypicture.jpg');
$entry->setContent('my great article with a picture <img src="https://old-url.test/mypicture.jpg" />');
$em->persist($entry);
$em->flush();
$tester = new CommandTester($command);
$tester->execute([
'old-url' => 'https://old-url.test',
]);
$this->assertStringContainsString('Finished updating.', $tester->getDisplay());
$entry = $em->getRepository(Entry::class)->findOneByUrl($url);
$this->assertSame($entry->getPreviewPicture(), $client->getContainer()->getParameter('domain_name') . '/mypicture.jpg');
$query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url');
$query->setParameter('url', $url);
$query->execute();
}
}