Completed
Push — master ( 8e1573...db6321 )
by
unknown
16:08 queued 08:39
created

FileProviderTest::testFileDownload()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 24
nc 1
nop 0
1
<?php
2
3
namespace MediaMonks\SonataMediaBundle\Tests\Functional;
4
5
use MediaMonks\SonataMediaBundle\Handler\SignatureParameterHandler;
6
use MediaMonks\SonataMediaBundle\Model\MediaInterface;
7
use MediaMonks\SonataMediaBundle\ParameterBag\DownloadParameterBag;
8
use Symfony\Component\DomCrawler\Crawler;
9
use Mockery as m;
10
11
class FileProviderTest extends AdminTestAbstract
12
{
13
    public function testFile()
14
    {
15
        $crawler = $this->uploadFile();
16
17
        $form = $crawler->selectButton('Update')->form();
18
19
        $this->assertSonataFormValues($form, ['title' => 'text']);
20
21
        $this->client->request('GET', self::BASE_PATH.'list');
22
        $this->assertContains('text', $this->client->getResponse()->getContent());
23
24
        $this->assertNumberOfFilesInPath(2, $this->getMediaPathPrivate());
25
    }
26
27
    public function testFileDownload()
28
    {
29
        $crawler = $this->uploadFile();
30
31
        $link = $crawler->selectLink('Download original')->link();
32
33
        ob_start();
34
        $this->client->click($link);
35
        $fileContents = $content = ob_get_contents();
36
        ob_end_clean();
37
38
        $this->assertEquals('foobar', $fileContents);
39
        $this->assertTrue($this->client->getResponse()->isSuccessful(), 'response status is 2xx');
40
41
        $media = m::mock(MediaInterface::class);
42
        $media->shouldReceive('getId')->andReturn(1);
43
44
        $parameterBag = new DownloadParameterBag();
45
        $signature = new SignatureParameterHandler(self::$kernel->getContainer()->getParameter('secret'));
46
        $parameters = $signature->getRouteParameters($media, $parameterBag);
47
48
        ob_start();
49
        $this->client->request(
50
            'GET',
51
            sprintf(
52
                '/media/download/%d?s=%s',
53
                $media->getId(),
54
                $parameters['s']
55
            )
56
        );
57
        $fileContents = $content = ob_get_contents();
58
        ob_end_clean();
59
60
        $this->assertEquals('foobar', $fileContents);
61
    }
62
63
    public function testInvalidFileUpload()
64
    {
65
        $this->uploadFile('text.exe');
66
67
        $this->assertContains('An error has occurred during the creation of item', $this->client->getResponse()->getContent());
68
        $this->assertContains('not allowed to upload a file with extension', $this->client->getResponse()->getContent());
69
    }
70
71
    public function testEmptyFileUpload()
72
    {
73
        $provider = 'file';
74
75
        $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider);
76
77
        $form = $crawler->selectButton('Create')->form();
78
79
        $this->assertSonataFormValues(
80
            $form,
81
            [
82
                'provider' => $provider,
83
            ]
84
        );
85
86
        $this->client->submit($form);
87
88
        $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent());
89
        $this->assertContains('This value should not be blank', $this->client->getResponse()->getContent());
90
    }
91
92
    /**
93
     * @param string $fileName
94
     * @return Crawler
95
     */
96
    private function uploadFile($fileName = 'text.txt')
97
    {
98
        $provider = 'file';
99
100
        $crawler = $this->client->request('GET', self::BASE_PATH.'create?provider='.$provider);
101
102
        $form = $crawler->selectButton('Create')->form();
103
104
        $this->assertSonataFormValues(
105
            $form,
106
            [
107
                'provider' => $provider,
108
            ]
109
        );
110
111
        $this->setFormBinaryContent($form, $this->getFixturesPath().$fileName);
112
113
        $crawler = $this->client->submit($form);
114
115
        return $crawler;
116
    }
117
}
118