wallabag/tests/Controller/SiteCredentialControllerTest.php

156 lines
5 KiB
PHP
Raw Normal View History

<?php
2024-02-19 00:30:12 +00:00
namespace Tests\Wallabag\Controller;
2022-08-28 00:01:46 +00:00
use Craue\ConfigBundle\Util\Config;
use Doctrine\ORM\EntityManagerInterface;
2023-08-07 23:21:56 +00:00
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Tests\Wallabag\WallabagTestCase;
2024-02-19 00:30:12 +00:00
use Wallabag\Entity\SiteCredential;
class SiteCredentialControllerTest extends WallabagTestCase
{
public function testAccessDeniedBecauseFeatureDisabled()
{
$this->logInAs('admin');
$client = $this->getTestClient();
2022-08-28 00:01:46 +00:00
$client->getContainer()->get(Config::class)->set('restricted_access', 0);
$client->request('GET', '/site-credentials/');
$this->assertSame(404, $client->getResponse()->getStatusCode());
2022-08-28 00:01:46 +00:00
$client->getContainer()->get(Config::class)->set('restricted_access', 1);
}
public function testListSiteCredential()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$crawler = $client->request('GET', '/site-credentials/');
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$body = $crawler->filter('body')->extract(['_text'])[0];
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('site_credential.description', $body);
$this->assertStringContainsString('site_credential.list.create_new_one', $body);
}
public function testNewSiteCredential()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$crawler = $client->request('GET', '/site-credentials/new');
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$body = $crawler->filter('body')->extract(['_text'])[0];
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('site_credential.new_site_credential', $body);
$this->assertStringContainsString('site_credential.form.back_to_list', $body);
$form = $crawler->filter('button[id=site_credential_save]')->form();
$data = [
'site_credential[host]' => 'google.io',
'site_credential[username]' => 'sergei',
'site_credential[password]' => 'microsoft',
];
$client->submit($form, $data);
2017-07-01 07:52:38 +00:00
$this->assertSame(302, $client->getResponse()->getStatusCode());
$crawler = $client->followRedirect();
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('flashes.site_credential.notice.added', $crawler->filter('body')->extract(['_text'])[0]);
}
public function testEditSiteCredential()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$credential = $this->createSiteCredential($client);
2017-07-01 07:52:38 +00:00
$crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$body = $crawler->filter('body')->extract(['_text'])[0];
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('site_credential.edit_site_credential', $body);
$this->assertStringContainsString('site_credential.form.back_to_list', $body);
$form = $crawler->filter('button[id=site_credential_save]')->form();
$data = [
'site_credential[host]' => 'google.io',
'site_credential[username]' => 'larry',
'site_credential[password]' => 'microsoft',
];
$client->submit($form, $data);
2017-07-01 07:52:38 +00:00
$this->assertSame(302, $client->getResponse()->getStatusCode());
$crawler = $client->followRedirect();
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('flashes.site_credential.notice.updated', $crawler->filter('body')->extract(['_text'])[0]);
}
public function testEditFromADifferentUserSiteCredential()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$credential = $this->createSiteCredential($client);
$this->logInAs('bob');
2017-07-01 07:52:38 +00:00
$client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
2017-07-01 07:52:38 +00:00
$this->assertSame(403, $client->getResponse()->getStatusCode());
}
public function testDeleteSiteCredential()
{
$this->logInAs('admin');
$client = $this->getTestClient();
$credential = $this->createSiteCredential($client);
2017-07-01 07:52:38 +00:00
$crawler = $client->request('GET', '/site-credentials/' . $credential->getId() . '/edit');
2017-07-01 07:52:38 +00:00
$this->assertSame(200, $client->getResponse()->getStatusCode());
$deleteForm = $crawler->filter('body')->selectButton('site_credential.form.delete')->form();
$client->submit($deleteForm, []);
2017-07-01 07:52:38 +00:00
$this->assertSame(302, $client->getResponse()->getStatusCode());
$crawler = $client->followRedirect();
2020-06-15 11:37:50 +00:00
$this->assertStringContainsString('flashes.site_credential.notice.deleted', $crawler->filter('body')->extract(['_text'])[0]);
}
2023-08-07 23:21:56 +00:00
private function createSiteCredential(KernelBrowser $client)
{
$credential = new SiteCredential($this->getLoggedInUser());
$credential->setHost('google.io');
$credential->setUsername('sergei');
$credential->setPassword('microsoft');
2022-08-28 00:01:46 +00:00
$em = $client->getContainer()->get(EntityManagerInterface::class);
$em->persist($credential);
$em->flush();
return $credential;
}
}