Skip to content

Commit e7c5597

Browse files
committed
minor #5550 Add more functional tests (javiereguiluz)
This PR was squashed before being merged into the 4.x branch. Discussion ---------- Add more functional tests Commits ------- 9b887ca Add more functional tests
2 parents f94dd9a + 9b887ca commit e7c5597

File tree

4 files changed

+63
-16
lines changed

4 files changed

+63
-16
lines changed

tests/Controller/CategoryCrudControllerTest.php

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\ORM\EntityManagerInterface;
66
use Doctrine\ORM\EntityRepository;
77
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
8+
use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
89
use EasyCorp\Bundle\EasyAdminBundle\Exception\ForbiddenActionException;
910
use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
1011
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Config\Action as AppAction;
@@ -27,6 +28,7 @@ protected function setUp(): void
2728
{
2829
$this->client = static::createClient();
2930
$this->client->followRedirects();
31+
$this->client->setServerParameters(['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
3032

3133
$container = static::getContainer();
3234
$this->adminUrlGenerator = $container->get(AdminUrlGenerator::class);
@@ -39,7 +41,7 @@ protected function setUp(): void
3941
*/
4042
public function testNew(?string $invalidCsrfToken, ?string $expectedErrorMessage)
4143
{
42-
$this->client->request('GET', $this->generateNewCategoryFormUrl(), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
44+
$this->client->request('GET', $this->generateNewCategoryFormUrl());
4345

4446
$form = [
4547
'Category[name]' => 'Foo',
@@ -80,13 +82,7 @@ public function new(): \Generator
8082
*/
8183
public function testEdit(?string $invalidCsrfToken, ?string $expectedErrorMessage)
8284
{
83-
$this->client->request(
84-
'GET',
85-
$this->generateEditCategoryFormUrl($this->categories->findOneBy([])->getId()),
86-
[],
87-
[],
88-
['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234'],
89-
);
85+
$this->client->request('GET', $this->generateEditCategoryFormUrl($this->categories->findOneBy([])->getId()));
9086

9187
$form = [
9288
'Category[name]' => 'Bar',
@@ -130,7 +126,7 @@ public function testDelete(?string $invalidCsrfToken, callable $expectedCategori
130126
$initialCategoriesCount = \count($this->categories->findAll());
131127

132128
// List all categories
133-
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl(), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
129+
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl());
134130
$this->assertResultCount($initialCategoriesCount);
135131

136132
// Try to delete the first found category
@@ -145,7 +141,7 @@ public function testDelete(?string $invalidCsrfToken, callable $expectedCategori
145141
$this->client->submit($form);
146142

147143
// List all categories again and see if the result count changed
148-
$this->client->request('GET', $this->generateCategoryIndexUrl(), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
144+
$this->client->request('GET', $this->generateCategoryIndexUrl());
149145
$this->assertResultCount($expectedCategoriesCount($initialCategoriesCount));
150146
}
151147

@@ -170,7 +166,7 @@ public function testDetail()
170166
/* @var Category $category */
171167
$category = $this->categories->findOneBy([]);
172168

173-
$this->client->request('GET', $this->generateCategoryDetailUrl($category->getId()), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
169+
$this->client->request('GET', $this->generateCategoryDetailUrl($category->getId()));
174170

175171
$this->assertSelectorTextContains('.form-panel-body', $category->getId());
176172
$this->assertSelectorTextContains('.form-panel-body', $category->getName());
@@ -191,7 +187,7 @@ public function testToggle(string $method, ?string $invalidCsrfToken, int $expec
191187
}
192188

193189
// Find the first toggle URL in the category list
194-
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl(), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
190+
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl());
195191
$firstFoundToggleUrl = $crawler->filter('td.field-boolean .form-switch input[type="checkbox"]')->first()->attr('data-toggle-url');
196192

197193
// Get the category's active state from the DB
@@ -225,6 +221,50 @@ public function testToggle(string $method, ?string $invalidCsrfToken, int $expec
225221
}
226222
}
227223

224+
public function testPagination()
225+
{
226+
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl());
227+
228+
$prevPageItem = $crawler->filter('.list-pagination-paginator .page-item:nth-child(1)');
229+
$prevPageLink = $prevPageItem->filter('.page-link');
230+
$firstPageItem = $crawler->filter('.list-pagination-paginator .page-item:nth-child(2)');
231+
$firstPageLink = $firstPageItem->filter('.page-link');
232+
$secondPageLink = $crawler->filter('.list-pagination-paginator .page-item:nth-child(3) .page-link');
233+
$nextPageItem = $crawler->filter('.list-pagination-paginator .page-item:nth-child(4)');
234+
$nextPageLink = $nextPageItem->filter('.page-link');
235+
236+
// test default pagination items
237+
$this->assertCount(4, $crawler->filter('.list-pagination-paginator .page-item'));
238+
$this->assertStringContainsString('Previous', $prevPageLink->text());
239+
$this->assertStringContainsString('disabled', $prevPageItem->attr('class'));
240+
$this->assertStringContainsString('1', $firstPageLink->text());
241+
$this->assertStringContainsString('active', $firstPageItem->attr('class'));
242+
$this->assertStringContainsString('2', $secondPageLink->text());
243+
$this->assertStringContainsString('Next', $nextPageLink->text());
244+
245+
// test default pagination URLs
246+
$firstPageUrl = $firstPageLink->attr('href');
247+
$this->assertSame('1', $this->getParameterFromUrlQueryString($firstPageUrl, EA::PAGE));
248+
249+
$secondPageUrl = $secondPageLink->attr('href');
250+
$this->assertSame('2', $this->getParameterFromUrlQueryString($secondPageUrl, EA::PAGE));
251+
252+
$nextPageUrl = $nextPageLink->attr('href');
253+
$this->assertSame($secondPageUrl, $nextPageUrl);
254+
255+
// test pagination maintains all query parameters, including custom ones
256+
$queryParameters = http_build_query(['sort[name]' => 'DESC', 'CUSTOM_param' => 'foobar1234']);
257+
$crawler = $this->client->request('GET', $this->generateCategoryIndexUrl().'&'.$queryParameters);
258+
259+
$firstPageUrl = $crawler->filter('.list-pagination-paginator .page-item:nth-child(2) .page-link')->attr('href');
260+
$this->assertSame(['name' => 'DESC'], $this->getParameterFromUrlQueryString($firstPageUrl, 'sort'));
261+
$this->assertSame('foobar1234', $this->getParameterFromUrlQueryString($firstPageUrl, 'CUSTOM_param'));
262+
263+
$nextPagePageUrl = $crawler->filter('.list-pagination-paginator .page-item:nth-child(4) .page-link')->attr('href');
264+
$this->assertSame(['name' => 'DESC'], $this->getParameterFromUrlQueryString($nextPagePageUrl, 'sort'));
265+
$this->assertSame('foobar1234', $this->getParameterFromUrlQueryString($nextPagePageUrl, 'CUSTOM_param'));
266+
}
267+
228268
public function toggle(): \Generator
229269
{
230270
yield [
@@ -257,7 +297,7 @@ public function testSearch(array $categories, string $query, int $expectedResult
257297
}
258298
$this->entityManager->flush();
259299

260-
$this->client->request('GET', $this->generateCategoryIndexUrl($query), [], [], ['PHP_AUTH_USER' => 'admin', 'PHP_AUTH_PW' => '1234']);
300+
$this->client->request('GET', $this->generateCategoryIndexUrl($query));
261301
$this->assertResultCount($expectedResultCount);
262302
}
263303

@@ -455,4 +495,12 @@ private function generateUsername(string $role): string
455495

456496
throw new \InvalidArgumentException(sprintf('Unknown role, use one of: %s', implode(', ', ['ROLE_USER', 'ROLE_ADMIN'])));
457497
}
498+
499+
private function getParameterFromUrlQueryString(string $url, string $parameterName): string|array|null
500+
{
501+
$queryString = parse_url($url, \PHP_URL_QUERY);
502+
parse_str($queryString, $queryStringParams);
503+
504+
return $queryStringParams[$parameterName] ?? null;
505+
}
458506
}

tests/TestApplication/config/packages/framework.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
$configuration = [
44
'secret' => 'F00',
55
'csrf_protection' => true,
6+
'http_method_override' => true,
67
'session' => [
78
'handler_id' => null,
89
'storage_factory_id' => 'session.storage.factory.mock_file',

tests/TestApplication/config/packages/security.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
use Symfony\Component\Security\Core\User\InMemoryUser;
44

55
$configuration = [
6-
'enable_authenticator_manager' => true,
7-
86
'password_hashers' => [
97
InMemoryUser::class => 'plaintext',
108
],

tests/TestApplication/src/DataFixtures/AppFixtures.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AppFixtures extends Fixture
1212
{
1313
public function load(ObjectManager $manager)
1414
{
15-
for ($i = 0; $i < 10; ++$i) {
15+
for ($i = 0; $i < 30; ++$i) {
1616
$category = (new Category())
1717
->setName('Category '.$i)
1818
->setSlug('category-'.$i);

0 commit comments

Comments
 (0)