1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-09-15 18:57:05 +00:00

Merge pull request #3137 from aaa2000/isolated-tests

Isolated tests
This commit is contained in:
Jérémy Benoist 2017-06-01 07:58:17 +02:00 committed by GitHub
commit 2a0eec07a5
23 changed files with 464 additions and 284 deletions

View file

@ -3,6 +3,7 @@
namespace Tests\Wallabag\ApiBundle\Controller;
use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
use Wallabag\ApiBundle\Entity\Client;
class DeveloperControllerTest extends WallabagCoreTestCase
{
@ -33,14 +34,10 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$this->assertContains('My app', $alert[0]);
}
/**
* @depends testCreateClient
*/
public function testCreateToken()
{
$client = $this->getClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$apiClient = $em->getRepository('WallabagApiBundle:Client')->findOneByName('My app');
$apiClient = $this->createApiClientForUser('admin');
$client->request('POST', '/oauth/v2/token', [
'grant_type' => 'password',
@ -83,6 +80,7 @@ class DeveloperControllerTest extends WallabagCoreTestCase
public function testRemoveClient()
{
$client = $this->getClient();
$adminApiClient = $this->createApiClientForUser('admin');
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
// Try to remove an admin's client with a wrong user
@ -90,12 +88,8 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$client->request('GET', '/developer');
$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');
$client->request('GET', '/developer/client/delete/'.$nbClients[0]->getId());
$client->request('GET', '/developer/client/delete/'.$adminApiClient->getId());
$this->assertEquals(403, $client->getResponse()->getStatusCode());
// Try to remove the admin's client with the good user
@ -111,7 +105,29 @@ class DeveloperControllerTest extends WallabagCoreTestCase
$client->click($link);
$this->assertEquals(302, $client->getResponse()->getStatusCode());
$newNbClients = $em->getRepository('WallabagApiBundle:Client')->findByUser($this->getLoggedInUserId());
$this->assertGreaterThan(count($newNbClients), count($nbClients));
$this->assertNull(
$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;
use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Helper\ContentProxy;
use Wallabag\UserBundle\Entity\User;
class EntryRestControllerTest extends WallabagApiTestCase
{
@ -811,22 +813,28 @@ class EntryRestControllerTest extends WallabagApiTestCase
public function testDeleteEntriesTagsListAction()
{
$entry = $this->client->getContainer()->get('doctrine.orm.entity_manager')
->getRepository('WallabagCoreBundle:Entry')
->findByUrlAndUserId('http://0.0.0.0/entry4', 1);
$em = $this->client->getContainer()->get('doctrine.orm.entity_manager');
$entry = new Entry($em->getReference(User::class, 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();
$this->assertCount(4, $tags);
$em->clear();
$list = [
[
'url' => 'http://0.0.0.0/entry4',
'tags' => 'new tag 1, new tag 2',
'url' => 'http://0.0.0.0/test-entry',
'tags' => 'foo-tag, bar-tag',
],
];
$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()
@ -851,9 +859,14 @@ class EntryRestControllerTest extends WallabagApiTestCase
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 = [
'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/entry3',
'http://0.0.0.0/test-entry1',
'http://0.0.0.0/test-entry-not-exist',
];
$this->client->request('DELETE', '/api/entries/list?urls='.json_encode($list));
@ -863,10 +876,10 @@ class EntryRestControllerTest extends WallabagApiTestCase
$content = json_decode($this->client->getResponse()->getContent(), true);
$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->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()

View file

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