Completed
Pull Request — master (#202)
by Ingo
02:38
created

AssetAdminTest_File::canEdit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace SilverStripe\AssetAdmin\Tests;
4
5
use SilverStripe\AssetAdmin\Controller\AssetAdmin;
6
use FunctionalTest;
7
use Versioned;
8
use File;
9
use AssetStoreTest_SpyStore;
10
use SS_HTTPRequest;
11
use Director;
12
use Injector;
13
14
15
/**
16
 * Tests {@see AssetAdmin}
17
 */
18
class AssetAdminTest extends FunctionalTest
19
{
20
21
    protected static $fixture_file = 'AssetAdminTest.yml';
22
23
    /**
24
     * @var Session
25
     */
26
    protected $session = null;
27
28
    public function setUp()
29
    {
30
        parent::setUp();
31
32
        AssetStoreTest_SpyStore::activate('AssetAdminTest');
33
        $memberID = $this->logInWithPermission('ADMIN');
34
        $this->session = Injector::inst()->create('Session', array('loggedInAs' => $memberID));
35
36
        // Create a test folders for each of the fixture references
37
        foreach (File::get()->filter('ClassName', 'Folder') as $folder) {
38
            /** @var Folder $folder */
39
            $folder->publish(Versioned::DRAFT, Versioned::LIVE);
40
        }
41
42
        // Create a test files for each of the fixture references
43
        $content = str_repeat('x', 1000000);
44
        foreach (File::get()->exclude('ClassName', 'Folder') as $file) {
45
            /** @var File $file */
46
            $file->setFromString($content, $file->generateFilename());
47
            $file->publish(Versioned::DRAFT, Versioned::LIVE);
0 ignored issues
show
Bug introduced by
The method publish() does not exist on File. Did you maybe mean onBeforePublish()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
48
        }
49
    }
50
51
    public function tearDown()
52
    {
53
        AssetStoreTest_SpyStore::reset();
54
        parent::tearDown();
55
    }
56
57
    /**
58
     * Tests filtering between date ranges
59
     */
60
    public function testItFiltersByDateInSearch()
61
    {
62
        $file1 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
63
        $file2 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file2');
64
65
        // Force creation times
66
        $file1->Created = '2014-01-05 23:11:39';
67
        $file1->write();
68
        $file2->Created = '2014-01-06 12:00:00';
69
        $file2->write();
70
71
        // Mock searches for 4th Jan
72
        $results = $this->getResultsForSearch([
73
            'CreatedFrom' => '2014-01-04',
74
            'CreatedTo' => '2014-01-04'
75
        ]);
76
        $this->assertEquals(count($results['files']), 0);
77
78
        // Mock searches for 5th Jan
79
        $results = $this->getResultsForSearch([
80
            'CreatedFrom' => '2014-01-05',
81
            'CreatedTo' => '2014-01-05'
82
        ]);
83
        $this->assertEquals(count($results['files']), 1);
84
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
85
86
87
        // Mock searches for 5th-6th Jan
88
        $results = $this->getResultsForSearch([
89
            'CreatedFrom' => '2014-01-05',
90
            'CreatedTo' => '2014-01-06'
91
        ]);
92
        $this->assertEquals(count($results['files']), 2);
93
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
94
        $this->assertContains($file2->ID, array_column($results['files'], 'id'));
95
96
        // Mock searches for 6th Jan
97
        $results = $this->getResultsForSearch([
98
            'CreatedFrom' => '2014-01-06',
99
            'CreatedTo' => '2014-01-06'
100
        ]);
101
        $this->assertEquals(count($results['files']), 1);
102
        $this->assertContains($file2->ID, array_column($results['files'], 'id'));
103
104
        // Mock searches for 7th Jan
105
        $results = $this->getResultsForSearch([
106
            'CreatedFrom' => '2014-01-07',
107
            'CreatedTo' => '2014-01-07'
108
        ]);
109
        $this->assertEquals(count($results['files']), 0);
110
    }
111
112
113
    public function testItDoesNotFilterByDefaultInSearch()
114
    {
115
        $rootfile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'rootfile');
116
        $file1 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
117
        $folder1 = $this->objFromFixture('Folder', 'folder1');
118
119
        $results = $this->getResultsForSearch();
120
        $this->assertContains($rootfile->ID, array_column($results['files'], 'id'),
121
            'Contains top level file'
122
        );
123
        $this->assertContains($folder1->ID, array_column($results['files'], 'id'),
124
            'Contains top level folder'
125
        );
126
        $this->assertContains($file1->ID, array_column($results['files'], 'id'),
127
            'Contains files in subfolder'
128
        );
129
    }
130
131
    public function testItFiltersByParentInSearch()
132
    {
133
        $file1 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
134
        $file2 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file2');
135
        $file1Folder = $file1->Parent();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on DataObject. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
136
        $file2Folder = $file2->Parent();
0 ignored issues
show
Bug introduced by
The method Parent() does not exist on DataObject. Did you maybe mean parentClass()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
137
138
        $results = $this->getResultsForSearch(['Name' => $file1->Name, 'ParentID' => $file1Folder->ID]);
139
        $this->assertEquals(count($results['files']), 1);
140
        $this->assertContains($file1->ID, array_column($results['files'], 'id'),
141
            'Returns file when contained in correct folder'
142
        );
143
144
        $results = $this->getResultsForSearch(['Name' => $file1->Name, 'ParentID' => $file2Folder->ID]);
145
        $this->assertEquals(count($results['files']), 0,
146
            'Does not return file when contained in different folder'
147
        );
148
    }
149
150
    public function testItFiltersByNameInSearch()
151
    {
152
        $file1 = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
153
154
        $results = $this->getResultsForSearch(['Name' => $file1->Name]);
155
        $this->assertEquals(count($results['files']), 1,
156
            'Finds by Name property'
157
        );
158
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
159
160
        $results = $this->getResultsForSearch(['Name' => 'First']);
161
        $this->assertEquals(count($results['files']), 1,
162
            'Finds by Title property'
163
        );
164
        $this->assertContains($file1->ID, array_column($results['files'], 'id'));
165
    }
166
167
    public function testItRestrictsViewInSearch()
168
    {
169
        $allowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
170
        $disallowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'disallowCanView');
171
172
        $results = $this->getResultsForSearch(['Name' => $allowedFile->Name]);
173
        $this->assertEquals(count($results['files']), 1);
174
        $this->assertContains($allowedFile->ID, array_column($results['files'], 'id'));
175
176
        $results = $this->getResultsForSearch(['Name' => $disallowedFile->Name]);
177
        $this->assertEquals(count($results['files']), 0);
178
    }
179
180
    public function testItRestrictsViewInReadFolder()
181
    {
182
        $folder1 = $this->objFromFixture('Folder', 'folder1');
183
        $allowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
184
        $disallowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'disallowCanView');
185
186
        $response = $this->get('admin/assets/api/readFolder?' . http_build_query(['id' => $folder1->ID]));
187
        $files = json_decode($response->getBody(), true);
188
        $this->assertArrayHasKey('files', $files);
189
        $ids = array_map(function ($file) {return $file['id'];}, $files['files']);
190
        $this->assertContains($allowedFile->ID, $ids);
191
        $this->assertEquals($allowedFile->ParentID, $folder1->ID);
192
        $this->assertNotContains($disallowedFile->ID, $ids);
193
        $this->assertEquals($disallowedFile->ParentID, $folder1->ID);
194
    }
195
196
    public function testItRestrictsUpdateFile()
197
    {
198
        $allowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
199
        $disallowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'disallowCanEdit');
200
201
        $response = Director::test(
202
            'admin/assets/api/updateFile',
203
            null,
204
            $this->session,
205
            'PUT',
206
            http_build_query(['id' => $allowedFile->ID, 'title' => 'new'])
207
        );
208
        $this->assertFalse($response->isError());
209
210
        $response = Director::test(
211
            'admin/assets/api/updateFile',
212
            null,
213
            $this->session,
214
            'PUT',
215
            http_build_query(['id' => $disallowedFile->ID, 'title' => 'new'])
216
        );
217
        $this->assertTrue($response->isError());
218
    }
219
220
    public function testItRestrictsDelete()
221
    {
222
        $allowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'file1');
223
        $disallowedFile = $this->objFromFixture('SilverStripe\AssetAdmin\Tests\AssetAdminTest_File', 'disallowCanDelete');
224
225
        $response = Director::test(
226
            'admin/assets/api/delete',
227
            null,
228
            $this->session,
229
            'DELETE',
230
            http_build_query(['ids' => [$allowedFile->ID, $disallowedFile->ID]])
231
        );
232
        $this->assertTrue($response->isError());
233
234
        $response = Director::test(
235
            'admin/assets/api/delete',
236
            null,
237
            $this->session,
238
            'DELETE',
239
            http_build_query(['ids' => [$allowedFile->ID]])
240
        );
241
        $this->assertFalse($response->isError());
242
    }
243
244
    /**
245
     * @param array $params
246
     * @return array
247
     */
248
    protected function getResultsForSearch($params = array())
249
    {
250
        $response = $this->get('admin/assets/api/search?' . http_build_query($params));
251
        $this->assertFalse($response->isError());
252
253
        return json_decode($response->getBody(), true);
254
    }
255
}
256
257
class AssetAdminTest_File extends File implements \TestOnly
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
258
{
259
    public function canView($member = null)
260
    {
261
        return ($this->Name != 'disallowCanView.txt');
262
    }
263
264
    public function canEdit($member = null)
265
    {
266
        return ($this->Name != 'disallowCanEdit.txt');
267
    }
268
269
    public function canDelete($member = null)
270
    {
271
        return ($this->Name != 'disallowCanDelete.txt');
272
    }
273
}
274