FilesystemPresenterTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 163
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 6 1
A testDefault() 0 8 1
A testDefaultWithPath() 0 10 1
A testDefaultDialog() 0 8 1
A testCreateDirectoryDialog() 0 9 1
B testUploadFile() 0 26 1
A createSystemThumbnail() 0 14 1
A testMakeDirectory() 0 10 1
B testRemove() 0 25 1
A testDownloadFile() 0 11 1
A testFilesDialog() 0 9 1
A testRegenerateThumbnails() 0 13 1
1
<?php
2
3
class FilesystemPresenterTest extends \WebCMS\Tests\PresenterTestCase
4
{
5
    public function setUp()
6
    {
7
        parent::setUp();
8
9
        $this->createPresenter('Admin:Filesystem');
10
    }
11
12
    public function testDefault()
13
    {
14
        $response = $this->makeRequest();
15
16
        $this->assertInstanceOf('Nette\Application\Responses\TextResponse', $response);
17
18
        $this->getResponse($response);
19
    }
20
21
    public function testDefaultWithPath()
22
    {
23
        mkdir('upload/test-directory');
24
25
        $response = $this->makeRequest('default', 'GET', array('path' => 'test-directory'));
26
27
        $this->assertInstanceOf('Nette\Application\Responses\TextResponse', $response);
28
29
        $this->getResponse($response);
30
    }
31
32
    public function testDefaultDialog()
33
    {
34
        $response = $this->makeRequest('default', 'GET', array('dialog' => 1));
35
36
        $this->assertInstanceOf('Nette\Application\Responses\TextResponse', $response);
37
38
        $this->getResponse($response);
39
    }
40
41
    public function testCreateDirectoryDialog()
42
    {
43
        $response = $this->makeRequest('default', 'GET', array(
44
            'action' => 'default',
45
                'do' => 'makeDirectory',
46
        ));
47
48
        $this->assertInstanceOf('Nette\Application\Responses\TextResponse', $response);
49
    }
50
51
    public function testUploadFile()
52
    {
53
        mkdir('upload/test-directory');
54
        system('convert -size 32x32 xc:white /tmp/empty.jpg');
55
56
        $this->createSystemThumbnail();
57
58
        $file = array(new \Nette\Http\FileUpload(array(
59
            'name' => 'empty.jpg',
60
            'tmp_name' => '/tmp/empty.jpg',
61
            'type' => 'text/plain',
62
            'size' => 0,
63
            'error' => 0,
64
        )));
65
66
        $response = $this->makeRequest('default', 'POST', array('path' => 'test-directory'), array(), 'uploadFile',
67
            array(
68
                    'file' => $file,
69
                )
70
            );
71
72
        $this->assertInstanceOf('Nette\Application\Responses\JsonResponse', $response);
73
74
        $this->assertFileExists('upload/test-directory/empty.jpg');
75
        $this->assertFileExists('thumbnails/test-directory/systemempty.jpg');
76
    }
77
78
    private function createSystemThumbnail()
79
    {
80
        $thumbnail = new \WebCMS\Entity\Thumbnail();
81
        $thumbnail->setKey('system');
82
        $thumbnail->setX('20');
83
        $thumbnail->setY('20');
84
        $thumbnail->setWatermark(false);
85
        $thumbnail->setSystem(true);
86
        $thumbnail->setResize(0);
87
        $thumbnail->setCrop(true);
88
89
        $this->em->persist($thumbnail);
90
        $this->em->flush();
91
    }
92
93
    public function testMakeDirectory()
94
    {
95
        $response = $this->makeRequest('default', 'GET', array(
96
            'action' => 'default',
97
            'name' => 'test directory',
98
        ), array(), 'makeDirectory');
99
100
        $this->assertInstanceOf('Nette\Application\Responses\TextResponse', $response);
101
        $this->assertEquals(true, file_exists('upload/test-directory/'));
102
    }
103
104
    public function testRemove()
105
    {
106
        mkdir('upload/test-directory');
107
        mkdir('thumbnails/test-directory');
108
        system('convert -size 32x32 xc:white upload/empty.jpg');
109
        system('convert -size 32x32 xc:white thumbnails/systemempty.jpg');
110
111
        $this->createSystemThumbnail();
112
113
        $this->assertEquals(true, file_exists('upload/test-directory/'));
114
        $this->assertEquals(true, file_exists('thumbnails/test-directory/'));
115
        $this->assertEquals(true, file_exists('thumbnails/test-directory/'));
116
117
        $this->assertEquals(true, file_exists('upload/empty.jpg'));
118
        $this->assertEquals(true, file_exists('thumbnails/systemempty.jpg'));
119
120
        $response = $this->makeRequest('default', 'GET', array(
121
            'action' => 'default',
122
            'pathToRemove' => '/empty.jpg',
123
        ), array(), 'deleteFile');
124
125
        $this->assertInstanceOf('Nette\Application\Responses\ForwardResponse', $response);
126
        $this->assertEquals(false, file_exists('upload/empty.jpg'));
127
        $this->assertEquals(false, file_exists('thumbnails/empty.jpg'));
128
    }
129
130
    public function testDownloadFile()
131
    {
132
        file_put_contents('upload/test.txt', 'Test text.');
133
134
        $response = $this->makeRequest('downloadFile', 'GET', array(
135
            'action' => 'downloadFile',
136
            'path' => 'test.txt',
137
        ));
138
139
        $this->assertInstanceOf('Nette\Application\Responses\FileResponse', $response);
140
    }
141
142
    public function testFilesDialog()
143
    {
144
        ob_start();
145
        $this->makeRequest('filesDialog');
146
        $response = ob_get_contents();
147
        ob_end_clean();
148
149
        $this->assertNotEmpty($response);
150
    }
151
152
    public function testRegenerateThumbnails()
153
    {
154
        $this->createSystemThumbnail();
155
156
        system('convert -size 32x32 xc:white upload/empty.jpg');
157
158
        $response = $this->makeRequest('filesDialog', 'GET', array(), array(), 'regenerateThumbnails');
159
160
        $this->assertInstanceOf('Nette\Application\Responses\ForwardResponse', $response);
161
162
        $this->assertEquals(true, file_exists('upload/empty.jpg'));
163
        $this->assertEquals(true, file_exists('thumbnails/systemempty.jpg'));
164
    }
165
}
166