diff --git a/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php b/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php index 429f529ab..dc8e723da 100644 --- a/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php +++ b/src/Wallabag/CoreBundle/Controller/SiteCredentialController.php @@ -24,9 +24,7 @@ class SiteCredentialController extends Controller */ public function indexAction() { - $em = $this->getDoctrine()->getManager(); - - $credentials = $em->getRepository('WallabagCoreBundle:SiteCredential')->findByUser($this->getUser()); + $credentials = $this->get('wallabag_core.site_credential_repository')->findByUser($this->getUser()); return $this->render('WallabagCoreBundle:SiteCredential:index.html.twig', array( 'credentials' => $credentials, diff --git a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php index ae69492d4..62a3bc131 100644 --- a/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php +++ b/src/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilder.php @@ -31,14 +31,13 @@ class GrabySiteConfigBuilder implements SiteConfigBuilder */ private $currentUser; - /** * GrabySiteConfigBuilder constructor. * * @param ConfigBuilder $grabyConfigBuilder - * @param TokenStorage $token + * @param TokenStorage $token * @param SiteCredentialRepository $credentialRepository - * @param LoggerInterface $logger + * @param LoggerInterface $logger */ public function __construct(ConfigBuilder $grabyConfigBuilder, TokenStorage $token, SiteCredentialRepository $credentialRepository, LoggerInterface $logger) { diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 8f5c372d8..104f60f17 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -5,6 +5,7 @@ namespace Tests\Wallabag\CoreBundle\Controller; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; use Wallabag\CoreBundle\Entity\Config; use Wallabag\CoreBundle\Entity\Entry; +use Wallabag\CoreBundle\Entity\SiteCredential; class EntryControllerTest extends WallabagCoreTestCase { @@ -1321,4 +1322,56 @@ class EntryControllerTest extends WallabagCoreTestCase $this->assertEquals($url, $content->getUrl()); $this->assertEquals($expectedLanguage, $content->getLanguage()); } + + /** + * This test will require an internet connection. + */ + public function testRestrictedArticle() + { + $url = 'http://www.monde-diplomatique.fr/2017/05/BONNET/57475'; + $this->logInAs('admin'); + $client = $this->getClient(); + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + + // enable restricted access + $client->getContainer()->get('craue_config')->set('restricted_access', 1); + + // create a new site_credential + $user = $client->getContainer()->get('security.token_storage')->getToken()->getUser(); + $credential = new SiteCredential($user); + $credential->setHost('monde-diplomatique.fr'); + $credential->setUsername('foo'); + $credential->setPassword('bar'); + + $em->persist($credential); + $em->flush(); + + $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()); + + $crawler = $client->followRedirect(); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + $this->assertContains('flashes.entry.notice.entry_saved', $crawler->filter('body')->extract(['_text'])[0]); + + $content = $em + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($url, $this->getLoggedInUserId()); + + $this->assertInstanceOf('Wallabag\CoreBundle\Entity\Entry', $content); + $this->assertSame('Crimes et réformes aux Philippines', $content->getTitle()); + + $client->getContainer()->get('craue_config')->set('restricted_access', 0); + } } diff --git a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php index 47bf0907a..7e6dafee6 100644 --- a/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/SiteCredentialControllerTest.php @@ -2,7 +2,9 @@ namespace Tests\Wallabag\CoreBundle\Controller; +use Symfony\Bundle\FrameworkBundle\Client; use Tests\Wallabag\CoreBundle\WallabagCoreTestCase; +use Wallabag\CoreBundle\Entity\SiteCredential; class SiteCredentialControllerTest extends WallabagCoreTestCase { @@ -52,18 +54,12 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]); } - /** - * @depends testNewSiteCredential - */ public function testEditSiteCredential() { $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); @@ -92,36 +88,26 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('larry', $crawler->filter('input[id=site_credential_username]')->attr('value')); } - /** - * @depends testNewSiteCredential - */ public function testEditFromADifferentUserSiteCredential() { - $this->logInAs('bob'); + $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); + + $this->logInAs('bob'); $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); $this->assertEquals(403, $client->getResponse()->getStatusCode()); } - /** - * @depends testNewSiteCredential - */ public function testDeleteSiteCredential() { $this->logInAs('admin'); $client = $this->getClient(); - $credential = $client->getContainer() - ->get('doctrine.orm.entity_manager') - ->getRepository('WallabagCoreBundle:SiteCredential') - ->findOneByHost('google.io'); + $credential = $this->createSiteCredential($client); $crawler = $client->request('GET', '/site-credentials/'.$credential->getId().'/edit'); @@ -137,4 +123,18 @@ class SiteCredentialControllerTest extends WallabagCoreTestCase $this->assertContains('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]); } + + private function createSiteCredential(Client $client) + { + $credential = new SiteCredential($this->getLoggedInUser()); + $credential->setHost('google.io'); + $credential->setUsername('sergei'); + $credential->setPassword('microsoft'); + + $em = $client->getContainer()->get('doctrine.orm.entity_manager'); + $em->persist($credential); + $em->flush(); + + return $credential; + } } diff --git a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php index 980f75793..1e1e8989b 100644 --- a/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php +++ b/tests/Wallabag/CoreBundle/GuzzleSiteAuthenticator/GrabySiteConfigBuilderTest.php @@ -25,7 +25,7 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $grabySiteConfig = new GrabySiteConfig(); $grabySiteConfig->requires_login = true; - $grabySiteConfig->login_uri = 'http://example.com/login'; + $grabySiteConfig->login_uri = 'http://www.example.com/login'; $grabySiteConfig->login_username_field = 'login'; $grabySiteConfig->login_password_field = 'password'; $grabySiteConfig->login_extra_fields = ['field=value']; @@ -67,13 +67,13 @@ class GrabySiteConfigBuilderTest extends PHPUnit_Framework_TestCase $logger ); - $config = $this->builder->buildForHost('example.com'); + $config = $this->builder->buildForHost('www.example.com'); $this->assertEquals( new SiteConfig([ 'host' => 'example.com', 'requiresLogin' => true, - 'loginUri' => 'http://example.com/login', + 'loginUri' => 'http://www.example.com/login', 'usernameField' => 'login', 'passwordField' => 'password', 'extraFields' => ['field' => 'value'],