-
Notifications
You must be signed in to change notification settings - Fork 171
/
Copy pathDrupalFilesTest.php
78 lines (68 loc) · 2.14 KB
/
DrupalFilesTest.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
<?php
namespace Drupal\Tests\common\Kernel\Util;
use Drupal\common\Util\DrupalFiles;
use Drupal\KernelTests\KernelTestBase;
/**
* @covers \Drupal\common\Util\DrupalFiles
* @coversDefaultClass \Drupal\common\Util\DrupalFiles
*
* @group dkan
* @group common
* @group kernel
*/
class DrupalFilesTest extends KernelTestBase {
protected static $modules = [
'common',
];
public function provideExceptions() {
return [
['Only file:// and http(s) urls are supported', 'badscheme://', 'any_destination'],
["Only moving files to Drupal's public directory (public://) is supported", 'file://', 'badscheme://'],
];
}
/**
* @covers ::retrieveFile
*
* @dataProvider provideExceptions
*/
public function testExceptions($exception_message, $url, $destination) {
/** @var \Drupal\common\Util\DrupalFiles $drupal_files */
$drupal_files = $this->container->get('dkan.common.drupal_files');
$this->expectException(\Exception::class);
$this->expectExceptionMessage($exception_message);
$drupal_files->retrieveFile($url, $destination);
}
public function provideRetrieve() {
return [
['http://'],
['https://'],
];
}
/**
* @covers ::retrieveFile
*
* @dataProvider provideRetrieve
*/
public function testHttpSource($url) {
// We're checking the internal logic of retrieveFile(), to make sure it
// calls systemRetrieveFile() given the inputs, and not testing whether the
// file is successfully retrieved.
// Mock a DrupalFiles object so that we can mock systemRetrieveFile().
$drupal_files = $this->getMockBuilder(DrupalFiles::class)
->setConstructorArgs([
$this->container->get('file_system'),
$this->container->get('stream_wrapper_manager'),
$this->container->get('http_client_factory'),
$this->container->get('messenger'),
])
->onlyMethods(['systemRetrieveFile'])
->getMock();
$drupal_files->expects($this->once())
->method('systemRetrieveFile')
->willReturn('/your/fake/path');
$this->assertEquals(
'/your/fake/path',
$drupal_files->retrieveFile($url, 'public://')
);
}
}