2015-02-08 22:05:51 +00:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
namespace Tests\Wallabag\CoreBundle;
|
2015-02-08 22:05:51 +00:00
|
|
|
|
2017-05-15 18:47:59 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Client;
|
|
|
|
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
2015-02-08 22:05:51 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
2016-06-24 09:55:45 +00:00
|
|
|
use Symfony\Component\BrowserKit\Cookie;
|
2017-05-15 18:47:59 +00:00
|
|
|
use Symfony\Component\Console\Input\ArrayInput;
|
|
|
|
use Symfony\Component\Console\Output\NullOutput;
|
|
|
|
use Wallabag\CoreBundle\Entity\Config;
|
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-02-08 22:05:51 +00:00
|
|
|
|
2015-03-29 08:53:10 +00:00
|
|
|
abstract class WallabagCoreTestCase extends WebTestCase
|
2015-02-08 22:05:51 +00:00
|
|
|
{
|
2017-05-15 18:47:59 +00:00
|
|
|
/**
|
|
|
|
* @var Client|null
|
|
|
|
*/
|
2015-02-08 22:05:51 +00:00
|
|
|
private $client = null;
|
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
2015-11-06 21:08:51 +00:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-02-08 22:05:51 +00:00
|
|
|
$this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2017-11-27 21:56:46 +00:00
|
|
|
public function getNewClient()
|
|
|
|
{
|
|
|
|
return $this->client = static::createClient();
|
|
|
|
}
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
public function getClient()
|
|
|
|
{
|
|
|
|
return $this->client;
|
|
|
|
}
|
|
|
|
|
2017-05-15 18:47:59 +00:00
|
|
|
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');
|
|
|
|
}
|
|
|
|
|
2016-06-24 09:55:45 +00:00
|
|
|
/**
|
|
|
|
* Login a user without making a HTTP request.
|
|
|
|
* If we make a HTTP request we lose ability to mock service in the container.
|
|
|
|
*
|
|
|
|
* @param string $username User to log in
|
|
|
|
*/
|
2015-02-10 21:32:42 +00:00
|
|
|
public function logInAs($username)
|
2016-06-24 09:55:45 +00:00
|
|
|
{
|
|
|
|
$container = $this->client->getContainer();
|
|
|
|
$session = $container->get('session');
|
|
|
|
|
|
|
|
$userManager = $container->get('fos_user.user_manager');
|
|
|
|
$loginManager = $container->get('fos_user.security.login_manager');
|
|
|
|
$firewallName = $container->getParameter('fos_user.firewall_name');
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$user = $userManager->findUserBy(['username' => $username]);
|
2017-05-15 18:47:59 +00:00
|
|
|
$loginManager->logInUser($firewallName, $user);
|
2016-06-24 09:55:45 +00:00
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$session->set('_security_' . $firewallName, serialize($container->get('security.token_storage')->getToken()));
|
2016-06-24 09:55:45 +00:00
|
|
|
$session->save();
|
|
|
|
|
|
|
|
$cookie = new Cookie($session->getName(), $session->getId());
|
|
|
|
$this->client->getCookieJar()->set($cookie);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instead of `logInAs` this method use a HTTP request to log in the user.
|
|
|
|
* Could be better for some tests.
|
|
|
|
*
|
|
|
|
* @param string $username User to log in
|
|
|
|
*/
|
|
|
|
public function logInAsUsingHttp($username)
|
2015-02-08 22:05:51 +00:00
|
|
|
{
|
|
|
|
$crawler = $this->client->request('GET', '/login');
|
|
|
|
$form = $crawler->filter('button[type=submit]')->form();
|
2016-04-12 09:36:01 +00:00
|
|
|
$data = [
|
2015-02-10 21:32:42 +00:00
|
|
|
'_username' => $username,
|
2015-02-17 20:03:23 +00:00
|
|
|
'_password' => 'mypassword',
|
2016-04-12 09:36:01 +00:00
|
|
|
];
|
2015-02-08 22:05:51 +00:00
|
|
|
|
|
|
|
$this->client->submit($form, $data);
|
|
|
|
}
|
2016-01-15 14:28:22 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-15 18:47:59 +00:00
|
|
|
* Return the user of the logged in user.
|
2016-01-15 14:28:22 +00:00
|
|
|
* You should be sure that you called `logInAs` before.
|
|
|
|
*
|
2017-05-15 18:47:59 +00:00
|
|
|
* @return User
|
2016-01-15 14:28:22 +00:00
|
|
|
*/
|
2017-05-15 18:47:59 +00:00
|
|
|
public function getLoggedInUser()
|
2016-01-15 14:28:22 +00:00
|
|
|
{
|
|
|
|
$token = static::$kernel->getContainer()->get('security.token_storage')->getToken();
|
|
|
|
|
|
|
|
if (null !== $token) {
|
2017-05-15 18:47:59 +00:00
|
|
|
return $token->getUser();
|
2016-01-15 14:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \RuntimeException('No logged in User.');
|
|
|
|
}
|
2016-09-24 17:57:59 +00:00
|
|
|
|
2017-05-15 18:47:59 +00:00
|
|
|
/**
|
|
|
|
* Return the user id of the logged in user.
|
|
|
|
* You should be sure that you called `logInAs` before.
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getLoggedInUserId()
|
|
|
|
{
|
|
|
|
return $this->getLoggedInUser()->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function useTheme($theme)
|
|
|
|
{
|
|
|
|
$config = $this->getEntityManager()->getRepository(Config::class)->findOneByUser($this->getLoggedInUser());
|
|
|
|
$config->setTheme($theme);
|
|
|
|
$this->getEntityManager()->persist($config);
|
|
|
|
$this->getEntityManager()->flush();
|
|
|
|
}
|
|
|
|
|
2016-09-24 17:57:59 +00:00
|
|
|
/**
|
|
|
|
* Check if Redis is installed.
|
2016-09-28 08:02:31 +00:00
|
|
|
* If not, mark test as skip.
|
2016-09-24 17:57:59 +00:00
|
|
|
*/
|
|
|
|
protected function checkRedis()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->client->getContainer()->get('wallabag_core.redis.client')->connect();
|
|
|
|
} catch (\Exception $e) {
|
2016-10-11 16:59:08 +00:00
|
|
|
$this->markTestSkipped('Redis is not installed/activated');
|
2016-09-24 17:57:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-02-08 22:05:51 +00:00
|
|
|
}
|