/
FileLinkHandlerTest.php
125 lines (115 loc) · 4.23 KB
/
FileLinkHandlerTest.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
<?php
declare(strict_types = 1);
namespace TYPO3\CMS\Core\Tests\Unit\LinkHandling;
/*
* This file is part of the TYPO3 CMS project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\LinkHandling\FileLinkHandler;
use TYPO3\CMS\Core\Resource\File;
use TYPO3\CMS\Core\Resource\ResourceFactory;
use TYPO3\CMS\Core\Resource\ResourceStorage;
use TYPO3\CMS\Core\Utility\MathUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class FileLinkHandlerTest extends UnitTestCase
{
/**
* testing folders
*/
/**
* Data provider for pointing to files
* t3:file:1:myfolder/myidentifier.jpg
* t3:folder:1:myfolder
*
* @return array
*/
public function resolveParametersForFilesDataProvider()
{
return [
'file without FAL - cool style' => [
[
'identifier' => 'fileadmin/deep/down.jpg'
],
[
'file' => 'fileadmin/deep/down.jpg'
],
't3://file?identifier=fileadmin%2Fdeep%2Fdown.jpg'
],
'file with FAL uid - cool style' => [
[
'uid' => 23
],
[
'file' => 23
],
't3://file?uid=23'
],
];
}
/**
* Helpful to know in which if() clause the stuff gets in
*
* @test
*
* @param string $input
* @param array $expected
* @param string $finalString
*
* @dataProvider resolveParametersForFilesDataProvider
*/
public function resolveFileReferencesToSplitParameters($input, $expected, $finalString)
{
/** @var ResourceStorage|\PHPUnit_Framework_MockObject_MockObject $storageMock */
$storage = $this->getMockBuilder(ResourceStorage::class)
->disableOriginalConstructor()
->getMock();
/** @var ResourceFactory|\PHPUnit_Framework_MockObject_MockObject $storageMock */
$factory = $this->getMockBuilder(ResourceFactory::class)
->disableOriginalConstructor()
->getMock();
// fake methods to return proper objects
$fileObject = new File(['identifier' => $expected['file'], 'name' => 'foobar.txt'], $storage);
$factory->expects($this->any())->method('getFileObject')->with($expected['file'])->willReturn($fileObject);
$factory->expects($this->any())->method('getFileObjectFromCombinedIdentifier')->with($expected['file'])->willReturn($fileObject);
$expected['file'] = $fileObject;
/** @var FileLinkHandler|\PHPUnit_Framework_MockObject_MockObject|\TYPO3\TestingFramework\Core\AccessibleObjectInterface $subject */
$subject = $this->getAccessibleMock(FileLinkHandler::class, ['dummy']);
$subject->_set('resourceFactory', $factory);
$this->assertEquals($expected, $subject->resolveHandlerData($input));
}
/**
* Helpful to know in which if() clause the stuff gets in
*
* @test
*
* @param string $input
* @param array $parameters
* @param string $expected
*
* @dataProvider resolveParametersForFilesDataProvider
*/
public function splitParametersToUnifiedIdentifierForFiles($input, $parameters, $expected)
{
$fileObject = $this->getMockBuilder(File::class)
->setMethods(['getUid', 'getIdentifier'])
->disableOriginalConstructor()
->getMock();
$uid = 0;
if (MathUtility::canBeInterpretedAsInteger($parameters['file'])) {
$uid = $parameters['file'];
}
$fileObject->expects($this->once())->method('getUid')->willReturn($uid);
$fileObject->expects($this->any())->method('getIdentifier')->willReturn($parameters['file']);
$parameters['file'] = $fileObject;
$subject = new FileLinkHandler();
$this->assertEquals($expected, $subject->asString($parameters));
}
}