forked from ezsystems/ezpublish-kernel
/
FileBaseIntegrationTest.php
219 lines (187 loc) · 5.9 KB
/
FileBaseIntegrationTest.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace eZ\Publish\SPI\Tests\FieldType;
use eZ\Publish\Core\FieldType;
use eZ\Publish\Core\IO\IOServiceInterface;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use FileSystemIterator;
use Symfony\Component\Filesystem\Filesystem as FilesystemComponent;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use eZ\Publish\Core\Base\Container\Compiler;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
abstract class FileBaseIntegrationTest extends BaseIntegrationTest
{
/**
* Temporary storage directory
*
* @var string
*/
protected static $tmpDir;
/** @var IOServiceInterface */
protected $ioService;
/**
* @see EZP-23534
*/
public function testLoadingContentWithMissingFileWorks()
{
$contentType = $this->createContentType();
$content = $this->createContent( $contentType, $this->getInitialValue() );
// delete the binary file object
$this->deleteStoredFile( $content );
// try loading the content again. It should work even though the image isn't physically here
$this->getCustomHandler()->contentHandler()->load( $content->versionInfo->contentInfo->id, 1 );
}
/**
* Deletes the binary file stored in the field
*
* @param $content
*
* @return mixed
*/
protected function deleteStoredFile( $content )
{
return $this->ioService->deleteBinaryFile(
$this->ioService->loadBinaryFile( $content->fields[1]->value->externalData['id'] )
);
}
/**
* Returns prefix used by the IOService
*
* @return string
*/
abstract protected function getStoragePrefix();
/**
* Sets up a temporary directory and stores its path in self::$tmpDir
*
* @return void
*/
public static function setUpBeforeClass()
{
$calledClass = get_called_class();
$tmpFile = tempnam(
sys_get_temp_dir(),
'eZ_' . substr( $calledClass, strrpos( $calledClass, '\\' ) + 1 )
);
// Convert file into directory
unlink( $tmpFile );
mkdir( $tmpFile );
self::$tmpDir = $tmpFile;
$storageDir = self::$tmpDir . '/var/ezdemo_site/storage';
if ( !file_exists( $storageDir ) )
{
$fs = new FilesystemComponent();
$fs->mkdir( $storageDir );
}
self::$setUp = false;
parent::setUpBeforeClass();
}
/**
* Removes the temp dir
*
* @return void
*/
public static function tearDownAfterClass()
{
self::removeRecursive( self::$tmpDir );
parent::tearDownAfterClass();
}
/**
* Removes the given directory path recursively
*
* @param string $dir
*
* @return void
*/
protected static function removeRecursive( $dir )
{
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$dir,
FileSystemIterator::KEY_AS_PATHNAME | FileSystemIterator::SKIP_DOTS | FileSystemIterator::CURRENT_AS_FILEINFO
),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ( $iterator as $path => $fileInfo )
{
if ( $fileInfo->isDir() )
{
rmdir( $path );
}
else
{
unlink( $path );
}
}
rmdir( $dir );
}
protected function getContainer()
{
$config = include __DIR__ . "/../../../../../config.php";
$installDir = $config["install_dir"];
$containerBuilder = new ContainerBuilder();
$settingsPath = $installDir . "/eZ/Publish/Core/settings/";
$loader = new YamlFileLoader( $containerBuilder, new FileLocator( $settingsPath ) );
$loader->load( 'fieldtypes.yml' );
$loader->load( 'io.yml' );
$loader->load( 'repository.yml' );
$loader->load( 'fieldtype_external_storages.yml' );
$loader->load( 'storage_engines/common.yml' );
$loader->load( 'storage_engines/shortcuts.yml' );
$loader->load( 'storage_engines/legacy.yml' );
$loader->load( 'search_engines/legacy.yml' );
$loader->load( 'storage_engines/cache.yml' );
$loader->load( 'settings.yml' );
$loader->load( 'fieldtype_services.yml' );
$loader->load( 'utils.yml' );
$containerBuilder->setParameter( "ezpublish.kernel.root_dir", $installDir );
$containerBuilder->setParameter(
"legacy_dsn",
$this->getDsn()
);
$containerBuilder->setParameter(
"io_root_dir",
self::$tmpDir . '/var/ezdemo_site/storage'
);
$containerBuilder->compile();
return $containerBuilder;
}
/**
* Asserts that the IO File with uri $uri exists
* @param string $uri
*/
protected function assertIOUriExists( $uri )
{
$this->assertTrue(
file_exists( self::$tmpDir . '/' . $uri ),
"Stored file uri $uri does not exist"
);
}
/**
* Asserts that the IO File with id $id exists
* @param string $id
*/
protected function assertIOIdExists( $id )
{
$path = $this->getPathFromId( $id );
$this->assertTrue(
file_exists( $path ),
"Stored file $path does not exists"
);
}
/**
* Returns the physical path to the file with id $id
*/
protected function getPathFromId( $id )
{
return $this->getStorageDir() . '/' . $this->getStoragePrefix() . '/' . $id;
}
protected function getStorageDir()
{
return ( self::$tmpDir ? self::$tmpDir . '/' : '' ) . self::$container->getParameter( 'storage_dir' );
}
protected function getFilesize( $binaryFileId )
{
return filesize( $this->getPathFromId( $binaryFileId ) );
}
}