Skip to content
This repository was archived by the owner on Mar 5, 2022. It is now read-only.

Commit 8dcab8a

Browse files
author
Florian Krämer
committed
Merge branch 'refs/heads/1.1.0' into 1.1
2 parents 3722fe9 + cc787f7 commit 8dcab8a

File tree

3 files changed

+104
-50
lines changed

3 files changed

+104
-50
lines changed

src/Storage/PathBuilder/BasePathBuilder.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,16 +257,16 @@ public function fullPath(EntityInterface $entity, array $options = []) {
257257
return $this->path($entity, $options) . $this->filename($entity, $options);
258258
}
259259

260-
/**
261-
* Builds the URL under which the file is accessible.
262-
*
263-
* This is for example important for S3 and Dropbox but also the Local adapter
264-
* if you symlink a folder to your webroot and allow direct access to a file.
265-
*
266-
* @param \Cake\Datasource\EntityInterface $entity
267-
* @param array $options
268-
* @return string
269-
*/
260+
/**
261+
* Builds the URL under which the file is accessible.
262+
*
263+
* This is for example important for S3 and Dropbox but also the Local adapter
264+
* if you symlink a folder to your webroot and allow direct access to a file.
265+
*
266+
* @param \Cake\Datasource\EntityInterface $entity
267+
* @param array $options
268+
* @return string
269+
*/
270270
public function url(EntityInterface $entity, array $options = []) {
271271
$url = $this->path($entity, $options) . $this->filename($entity, $options);
272272
return str_replace('\\', '/', $url);
@@ -280,6 +280,7 @@ public function url(EntityInterface $entity, array $options = []) {
280280
* @param string $string Input string
281281
* @param int $level Depth of the path to generate.
282282
* @param string $method Hash method, crc32 or sha1.
283+
* @throws \InvalidArgumentException
283284
* @return string
284285
*/
285286
public function randomPath($string, $level = 3, $method = 'sha1') {
@@ -293,6 +294,7 @@ public function randomPath($string, $level = 3, $method = 'sha1') {
293294
if (method_exists($this, $method)) {
294295
return $this->{$method}($string, $level);
295296
}
297+
throw new \InvalidArgumentException(sprintf('BasepathBuilder::randomPath() invalid hash `%s` method provided!', $method));
296298
}
297299

298300
/**
@@ -301,6 +303,7 @@ public function randomPath($string, $level = 3, $method = 'sha1') {
301303
* Please STOP USING CR32! See the huge warning on the php documentation page.
302304
* of the crc32() function.
303305
*
306+
* @deprecated Stop using it, see the methods doc block for more info.
304307
* @link http://php.net/manual/en/function.crc32.php
305308
* @link https://www.box.com/blog/crc32-checksums-the-good-the-bad-and-the-ugly/
306309
* @param string $string Input string

src/Storage/StorageUtils.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,11 @@ public static function ksortRecursive(&$array, $sortFlags = SORT_REGULAR) {
161161
/**
162162
* Returns an array that matches the structure of a regular upload for a local file
163163
*
164-
* @param $file
165-
* @param string File with path
164+
* @param $file The file you want to get an upload array for.
165+
* @param string Name of the file to use in the upload array.
166166
* @return array Array that matches the structure of a regular upload
167167
*/
168-
public static function uploadArray($file, $filename = null) {
168+
public static function fileToUploadArray($file, $filename = null) {
169169
$File = new File($file);
170170
if (empty($fileName)) {
171171
$filename = basename($file);
@@ -179,6 +179,17 @@ public static function uploadArray($file, $filename = null) {
179179
];
180180
}
181181

182+
/**
183+
* Convenience alias for fileToUploadArray
184+
*
185+
* @param $file
186+
* @param string File with path
187+
* @return array Array that matches the structure of a regular upload
188+
*/
189+
public static function uploadArray($file, $filename = null) {
190+
return self::fileToUploadArray($file, $filename);
191+
}
192+
182193
/**
183194
* Gets the hash of a file.
184195
*

tests/TestCase/Storage/PathBuilder/BasePathBuilderTest.php

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
use Cake\ORM\TableRegistry;
77
use Cake\TestSuite\TestCase;
88

9+
class TestBasePathBuilder extends BasePathBuilder {
10+
11+
/**
12+
*
13+
*/
14+
public function randomPathTestMethod($string) {
15+
return $string . 'test';
16+
}
17+
}
18+
919
class BasePathBuilderTest extends TestCase {
1020

11-
/**
12-
* Fixtures
13-
*
14-
* @var array
15-
*/
21+
/**
22+
* Fixtures
23+
*
24+
* @var array
25+
*/
1626
public $fixtures = array(
1727
'plugin.Burzum\FileStorage.FileStorage'
1828
);
@@ -36,11 +46,11 @@ public function setUp() {
3646
$this->entity->accessible('id', true);
3747
}
3848

39-
/**
40-
* testPathbuilding
41-
*
42-
* @return void
43-
*/
49+
/**
50+
* testPathbuilding
51+
*
52+
* @return void
53+
*/
4454
public function testPathbuilding() {
4555
$builder = new BasePathBuilder();
4656
$config = $builder->config();
@@ -73,30 +83,60 @@ public function testPathbuilding() {
7383
$result = $builder->path($this->entity);
7484
$this->assertEquals($result, '14' . DS . '83' . DS . '23' . DS . 'filestorage1' . DS . 'files' . DS);
7585

86+
$builder->config($config);
87+
$builder->config('pathPrefix', 'files');
88+
$result = $builder->path($this->entity);
89+
$this->assertEquals($result, 'files' . DS . '14' . DS . '83' . DS . '23' . DS . 'filestorage1' . DS);
90+
91+
$builder->config($config);
7692
$result = $builder->url($this->entity);
77-
$expected = '14/83/23/filestorage1/files/filestorage1.png';
93+
$expected = '14/83/23/filestorage1/filestorage1.png';
7894
$this->assertEquals($result, $expected);
7995
}
8096

81-
/**
82-
* testRandomPath
83-
*
84-
* @return void
85-
*/
97+
/**
98+
* testRandomPath
99+
*
100+
* @return void
101+
*/
86102
public function testRandomPath() {
87-
$builder = new BasePathBuilder();
103+
$builder = new TestBasePathBuilder();
88104
$result = $builder->randomPath('test', 5, 'sha1');
89105
$this->assertEquals($result, '4a' . DS . '8f' . DS . 'e5' . DS . 'cc' . DS . 'b1' . DS);
90106

91107
$result = $builder->randomPath('test', 3, 'sha1');
92108
$this->assertEquals($result, '4a' . DS . '8f' . DS . 'e5' . DS);
109+
110+
if (PHP_INT_SIZE === 4) {
111+
$result = $builder->randomPath('test', 3, 'crc32');
112+
$this->assertEquals($result, '00' . DS . '33' . DS . '73' . DS);
113+
}
114+
115+
if (PHP_INT_SIZE === 8) {
116+
$result = $builder->randomPath('test', 3, 'crc32');
117+
$this->assertEquals($result, '96' . DS . '39' . DS . '23' . DS);
118+
}
119+
120+
$result = $builder->randomPath('test', 3, 'randomPathTestMethod');
121+
$this->assertEquals($result, 'testtest');
122+
}
123+
124+
/**
125+
* testRandomPathInvalidArgumentException
126+
*
127+
* @expectedException \InvalidArgumentException
128+
* @return void
129+
*/
130+
public function testRandomPathInvalidArgumentException() {
131+
$builder = new BasePathBuilder();
132+
$result = $builder->randomPath('test', 5, 'does-not-exist');
93133
}
94134

95-
/**
96-
* testEnsureSlash
97-
*
98-
* @return void
99-
*/
135+
/**
136+
* testEnsureSlash
137+
*
138+
* @return void
139+
*/
100140
public function testEnsureSlash() {
101141
$string = 'foo/bar';
102142
$builder = new BasePathBuilder();
@@ -107,22 +147,22 @@ public function testEnsureSlash() {
107147
$this->assertEquals($result, DS . $string . DS);
108148
}
109149

110-
/**
111-
* testEnsureSlashInvalidArgumentException
112-
*
113-
* @expectedException \InvalidArgumentException
114-
*/
150+
/**
151+
* testEnsureSlashInvalidArgumentException
152+
*
153+
* @expectedException \InvalidArgumentException
154+
*/
115155
public function testEnsureSlashInvalidArgumentException() {
116156
$string = 'foo/bar';
117157
$builder = new BasePathBuilder();
118158
$builder->ensureSlash($string, 'INVALID!');
119159
}
120160

121-
/**
122-
* testSplitFilename
123-
*
124-
* @return void
125-
*/
161+
/**
162+
* testSplitFilename
163+
*
164+
* @return void
165+
*/
126166
public function testSplitFilename() {
127167
$builder = new BasePathBuilder();
128168
$result = $builder->splitFilename('some.fancy.name.jpg');
@@ -140,11 +180,11 @@ public function testSplitFilename() {
140180
$this->assertEquals($result, $expected);
141181
}
142182

143-
/**
144-
* testStripDashes
145-
*
146-
* @return void
147-
*/
183+
/**
184+
* testStripDashes
185+
*
186+
* @return void
187+
*/
148188
public function testStripDashes() {
149189
$builder = new BasePathBuilder();
150190
$result = $builder->stripDashes('with-dashes-!');

0 commit comments

Comments
 (0)