-
Notifications
You must be signed in to change notification settings - Fork 21
/
UploadableBehaviorTest.php
201 lines (180 loc) · 6 KB
/
UploadableBehaviorTest.php
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
199
200
201
<?php
/**
* BEdita, API-first content management framework
* Copyright 2017 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* See LICENSE.LGPL or <http://gnu.org/licenses/lgpl-3.0.html> for more details.
*/
namespace BEdita\Core\Test\TestCase\Model\Behavior;
use BEdita\Core\Filesystem\FilesystemRegistry;
use BEdita\Core\Test\Utility\TestFilesystemTrait;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
/**
* @coversDefaultClass \BEdita\Core\Model\Behavior\UploadableBehavior
*/
class UploadableBehaviorTest extends TestCase
{
use TestFilesystemTrait;
/**
* Test subject
*
* @var \BEdita\Core\Model\Table\StreamsTable
*/
protected $Streams;
/**
* Fixtures
*
* @var array
*/
public $fixtures = [
'plugin.BEdita/Core.ObjectTypes',
'plugin.BEdita/Core.Relations',
'plugin.BEdita/Core.RelationTypes',
'plugin.BEdita/Core.Objects',
'plugin.BEdita/Core.Streams',
];
/**
* @inheritDoc
*/
public function setUp(): void
{
parent::setUp();
$this->filesystemSetup();
$this->Streams = TableRegistry::getTableLocator()->get('Streams');
}
/**
* @inheritDoc
*/
public function tearDown(): void
{
$this->filesystemRestore();
unset($this->Streams);
parent::tearDown();
}
/**
* Data provider for `testAfterSave` test case.
*
* @return array
*/
public function afterSaveProvider(): array
{
$originalContents = "Sample uploaded file.\n";
$newContents = 'Modified contents.';
return [
'nothing to do' => [
[
'default://9e58fa47-db64-4479-a0ab-88a706180d59.txt' => $originalContents,
],
[
'version' => 99, // Update some useless field so that the save is actually triggered.
],
],
'updated contents' => [
[
'default://9e58fa47-db64-4479-a0ab-88a706180d59.txt' => $newContents,
],
[
'contents' => $newContents,
],
],
'renamed file' => [
[
'default://9e58fa47-db64-4479-a0ab-88a706180d59.txt' => false,
'default://9e58fa47-db64-4479-a0ab-88a706180d59-new-file.txt' => $originalContents,
],
[
'file_name' => 'new-file.txt',
],
],
'updated contents and renamed file' => [
[
'default://9e58fa47-db64-4479-a0ab-88a706180d59.txt' => false,
'default://9e58fa47-db64-4479-a0ab-88a706180d59-new-file.txt' => $newContents,
],
[
'file_name' => 'new-file.txt',
'contents' => $newContents,
],
],
'private file' => [
[
'default://9e58fa47-db64-4479-a0ab-88a706180d59-new-file.txt' => $newContents,
],
[
'file_name' => 'new-file.txt',
'contents' => $newContents,
'private_url' => true,
],
],
];
}
/**
* Test file management after the entity is saved.
*
* @param array $expected Expected files on filesystem and their contents.
* @param array $data Data to patch entity with.
* @return void
* @dataProvider afterSaveProvider()
* @covers ::afterSave()
* @covers ::processUpload()
* @covers ::setVisibility()
* @covers ::write()
*/
public function testAfterSave(array $expected, array $data): void
{
$manager = FilesystemRegistry::getMountManager();
$stream = $this->Streams->get('9e58fa47-db64-4479-a0ab-88a706180d59');
$this->Streams->patchEntity($stream, $data, ['accessibleFields' => ['*' => true]]);
if ($stream->isDirty('file_name')) {
$stream->uri = $stream->filesystemPath(); // Force update of URI.
}
$this->Streams->saveOrFail($stream);
$visibility = (bool)$stream->get('private_url') ? 'private' : 'public';
foreach ($expected as $path => $contents) {
if ($contents === false) {
static::assertFalse($manager->has($path));
} else {
static::assertTrue($manager->has($path));
static::assertSame($contents, $manager->read($path));
static::assertEquals($visibility, $manager->getVisibility($path));
}
}
}
/**
* Test file management after the entity is delete.
*
* @return void
* @covers ::afterDelete()
* @covers ::processDelete()
*/
public function testAfterDelete(): void
{
$manager = FilesystemRegistry::getMountManager();
$stream = $this->Streams->get('9e58fa47-db64-4479-a0ab-88a706180d59');
$path = $stream->uri;
$this->Streams->deleteOrFail($stream);
static::assertFalse($manager->has($path));
}
/**
* Test [@see \BEdita\Core\Model\Behavior\UploadableBehavior::copyFiles()} method..
*
* @return void
* @covers ::copyFiles()
*/
public function testCopyFiles(): void
{
$manager = FilesystemRegistry::getMountManager();
$stream = $this->Streams->get('9e58fa47-db64-4479-a0ab-88a706180d59');
$copy = $this->Streams->newEntity([]);
$copy->uri = $copy->filesystemPath();
$this->Streams->copyFiles($stream, $copy);
static::assertTrue($manager->has($copy->uri));
static::assertSame($manager->read($stream->uri), $manager->read($copy->uri));
}
}