-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMediaAdminControllerTest.php
More file actions
198 lines (158 loc) · 9.16 KB
/
MediaAdminControllerTest.php
File metadata and controls
198 lines (158 loc) · 9.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace JoliCode\MediaBundle\Tests\Bridge\EasyAdmin\Controller;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
use JoliCode\MediaBundle\Tests\Application\Entity\Page;
use JoliCode\MediaBundle\Tests\Application\Kernel;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
class MediaAdminControllerTest extends WebTestCase
{
private KernelBrowser $client;
private EntityRepository $pageRepository;
protected function setUp(): void
{
parent::setUp();
$this->client = static::createClient();
$container = static::getContainer();
/** @var EntityManagerInterface */
$entityManager = $container->get(EntityManagerInterface::class);
$this->pageRepository = $entityManager->getRepository(Page::class);
if (self::$kernel instanceof Kernel) {
$application = new Application(self::$kernel);
$application->setAutoExit(false);
$application->run(new StringInput('doctrine:fixtures:load --purge-with-truncate --no-interaction --quiet'));
}
}
public function testDelete(): void
{
// MediaDeleteBehavior::RESTRICT
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore');
$media = $this->findInGalleryFromName($crawler, 'restrict.pdf');
$crawler = $this->client->click($media->link());
$this->assertResponseIsSuccessful();
$this->client->submit($crawler->filter('form[name="delete"]')->form());
$this->assertResponseRedirects('/admin?routeName=joli_media_easy_admin_show&routeParams%5Bkey%5D=restrict.pdf');
$this->client->followRedirect();
$this->assertSelectorTextContains('.alert-danger', 'The media "restrict.pdf" is used in the "mediaRestrict" field of the "JoliCode\MediaBundle\Tests\Application\Entity\Page" entity. It cannot be deleted.');
// MediaDeleteBehavior::SET_NULL
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore');
$media = $this->findInGalleryFromName($crawler, 'set_null.pdf');
$crawler = $this->client->click($media->link());
$this->assertResponseIsSuccessful();
$this->client->submit($crawler->filter('form[name="delete"]')->form());
$this->assertResponseRedirects('/admin?routeName=joli_media_easy_admin_explore&routeParams%5Bkey%5D=.');
$crawler = $this->client->followRedirect();
$this->assertSelectorTextContains('.alert-success', 'Media "set_null.pdf" deleted successfully');
$this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_show&routeParams%5Bkey%5D=set_null.pdf');
$this->assertResponseStatusCodeSame(404);
/** @var Page $page */
$page = $this->pageRepository->findOneBy(['title' => 'Page 1']);
$this->assertNull($page->getMediaSetNull());
// no MediaDeleteBahavior defined
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore');
$media = $this->findInGalleryFromName($crawler, 'default.pdf');
$crawler = $this->client->click($media->link());
$this->assertResponseIsSuccessful();
$this->client->submit($crawler->filter('form[name="delete"]')->form());
$this->assertResponseRedirects('/admin?routeName=joli_media_easy_admin_explore&routeParams%5Bkey%5D=.');
$this->client->followRedirect();
$this->assertSelectorTextContains('.alert-success', 'Media "default.pdf" deleted successfully');
$this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_show&routeParams%5Bkey%5D=default.pdf');
$this->assertResponseStatusCodeSame(404);
/** @var Page $page */
$page = $this->pageRepository->findOneBy(['title' => 'Page 1']);
$this->assertSame('default.pdf', $page->getMediaDefault()->getPath());
}
public function testUploadAtRoot(): void
{
// test upload at the root of the media library
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore');
$this->assertResponseIsSuccessful();
$this->assertSelectorExists('form[name="upload"]');
$form = $crawler->filter('form[name="upload"]')->form();
$tmpFile = tempnam(sys_get_temp_dir(), 'upload-test');
copy(__DIR__ . '/../../../../fixtures/circle-pattern.png', $tmpFile);
$phpValues = $form->getPhpValues();
unset($phpValues['upload']['path']);
$this->client->request($form->getMethod(), $form->getUri(), $phpValues, [
'upload' => [
'file' => new UploadedFile(
$tmpFile,
'circle-pattern.png',
'image/png',
null,
true,
),
],
]);
$this->assertResponseFormatSame('json');
$this->assertResponseIsSuccessful();
$responseContent = $this->client->getResponse()->getContent();
$this->assertIsString($responseContent);
$response = json_decode((string) $responseContent, true);
$this->assertArrayHasKey('files', $response);
$this->assertCount(1, $response['files']);
$this->assertArrayHasKey('name', $response['files'][0]);
$this->assertArrayHasKey('url', $response['files'][0]);
$this->assertArrayHasKey('size', $response['files'][0]);
$this->assertArrayHasKey('type', $response['files'][0]);
$this->assertArrayHasKey('thumbnailUrl', $response['files'][0]);
$this->assertSame('circle-pattern.png', $response['files'][0]['name']);
$this->assertSame('/media/original/circle-pattern.png', $response['files'][0]['url']);
$this->assertSame(62563, $response['files'][0]['size']);
$this->assertSame('image/png', $response['files'][0]['type']);
$this->assertSame('/media/cache/joli-media-easy-admin/circle-pattern.png', $response['files'][0]['thumbnailUrl']);
}
public function testViewMode(): void
{
// test switching view mode between grid and list
$this->client->followRedirects();
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore');
$gridViewLink = $crawler->selectLink('Grid view');
$this->assertStringContainsString('active', (string) $gridViewLink->attr('class')); // grid is default
$this->assertSelectorCount(5, '.gallery-grid-item');
$this->assertSelectorNotExists('.gallery-list-item');
$listViewLink = $crawler->selectLink('List view');
$this->assertStringNotContainsString('active', (string) $listViewLink->attr('class'));
$crawler = $this->client->click($listViewLink->link());
$listViewLink = $crawler->selectLink('List view');
$this->assertStringContainsString('active', (string) $listViewLink->attr('class'));
$this->assertSelectorNotExists('.gallery-grid-item');
$this->assertSelectorCount(5, '.gallery-list-item');
$gridViewLink = $crawler->selectLink('Grid view');
$this->assertStringNotContainsString('active', (string) $gridViewLink->attr('class'));
$crawler = $this->client->click($gridViewLink->link()); // back to grid
$this->assertStringContainsString('active', (string) $crawler->selectLink('Grid view')->attr('class'));
$this->assertSelectorCount(5, '.gallery-grid-item');
$this->assertSelectorNotExists('.gallery-list-item');
// test the view mode inside a subfolder
$crawler = $this->client->request(Request::METHOD_GET, '/admin?routeName=joli_media_easy_admin_explore&routeParams%5Bkey%5D=/sub/folder');
$gridViewLink = $crawler->selectLink('Grid view');
$this->assertStringContainsString('active', (string) $gridViewLink->attr('class')); // grid is default
$this->assertSelectorCount(3, '.gallery-grid-item');
$this->assertSelectorNotExists('.gallery-list-item');
$listViewLink = $crawler->selectLink('List view');
$this->assertStringNotContainsString('active', (string) $listViewLink->attr('class'));
$crawler = $this->client->click($listViewLink->link());
$listViewLink = $crawler->selectLink('List view');
$this->assertStringContainsString('active', (string) $listViewLink->attr('class'));
$this->assertSelectorNotExists('.gallery-grid-item');
$this->assertSelectorCount(3, '.gallery-list-item');
}
protected static function getKernelClass(): string
{
return Kernel::class;
}
private function findInGalleryFromName(Crawler $crawler, string $name): Crawler
{
return $crawler->filter('ul.gallery-grid--files .gallery-grid-item__link')
->reduce(static fn (Crawler $node): bool => str_contains((string) $node->filter('.gallery-grid-item__name')->text(), $name))
;
}
}