23
23
*/
24
24
class DownloadTest extends AbstractBackendController
25
25
{
26
- /**
27
- * @var WriteInterface
28
- */
29
- private $ varDirectory ;
30
-
31
- /**
32
- * @var string
33
- */
34
- private $ fullDirectoryPath ;
35
-
36
26
/**
37
27
* @var string
38
28
*/
@@ -43,11 +33,6 @@ class DownloadTest extends AbstractBackendController
43
33
*/
44
34
private $ filesize ;
45
35
46
- /**
47
- * @var Filesystem
48
- */
49
- private $ filesystem ;
50
-
51
36
/**
52
37
* @var Auth
53
38
*/
@@ -65,17 +50,20 @@ protected function setUp()
65
50
{
66
51
parent ::setUp ();
67
52
68
- $ this ->filesystem = $ this ->_objectManager ->get (Filesystem::class);
69
- $ this ->auth = $ this ->_objectManager ->get (Auth::class);
53
+ $ filesystem = $ this ->_objectManager ->get (Filesystem::class);
54
+ $ auth = $ this ->_objectManager ->get (Auth::class);
55
+ $ auth ->getAuthStorage ()->setIsFirstPageAfterLogin (false );
70
56
$ this ->backendUrl = $ this ->_objectManager ->get (BackendUrl::class);
71
- $ baseDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::ROOT );
72
- $ this ->varDirectory = $ this ->filesystem ->getDirectoryWrite (DirectoryList::VAR_DIR );
73
- $ this ->varDirectory ->create ($ this ->varDirectory ->getRelativePath ('export ' ));
74
- $ this ->fullDirectoryPath = $ this ->varDirectory ->getAbsolutePath ('export ' );
75
- $ filePath = $ this ->fullDirectoryPath . DIRECTORY_SEPARATOR . $ this ->fileName ;
76
- $ fixtureDir = realpath (__DIR__ . '/../../Import/_files ' );
77
- $ baseDirectory ->copyFile ($ fixtureDir . '/ ' . $ this ->fileName , $ filePath );
78
- $ this ->filesize = filesize ($ filePath );
57
+ $ this ->backendUrl ->turnOnSecretKey ();
58
+
59
+ $ sourceFilePath = __DIR__ . '/../../Import/_files ' . DIRECTORY_SEPARATOR . $ this ->fileName ;
60
+ $ destinationFilePath = 'export ' . DIRECTORY_SEPARATOR . $ this ->fileName ;
61
+ //Refers to tests 'var' directory
62
+ $ varDirectory = $ filesystem ->getDirectoryRead (DirectoryList::VAR_DIR );
63
+ //Refers to application root directory
64
+ $ rootDirectory = $ filesystem ->getDirectoryWrite (DirectoryList::ROOT );
65
+ $ rootDirectory ->copyFile ($ sourceFilePath , $ varDirectory ->getAbsolutePath ($ destinationFilePath ));
66
+ $ this ->filesize = $ varDirectory ->stat ($ destinationFilePath )['size ' ];
79
67
}
80
68
81
69
/**
@@ -87,10 +75,18 @@ protected function setUp()
87
75
*/
88
76
public function testExecute (): void
89
77
{
90
- $ uri = 'backend/admin/export_file/download/filename/ ' . $ this ->fileName ;
91
- $ this ->prepareRequest ($ uri );
78
+ $ request = $ this ->getRequest ();
79
+ list ($ routeName , $ controllerName , $ actionName ) = explode ('/ ' , Download::URL );
80
+ $ request ->setMethod (Http::METHOD_GET )
81
+ ->setRouteName ($ routeName )
82
+ ->setControllerName ($ controllerName )
83
+ ->setActionName ($ actionName );
84
+ $ request ->setParam ('filename ' , $ this ->fileName );
85
+ $ request ->setParam (BackendUrl::SECRET_KEY_PARAM_NAME , $ this ->backendUrl ->getSecretKey ());
92
86
93
- $ this ->dispatch ($ uri );
87
+ ob_start ();
88
+ $ this ->dispatch ('backend/admin/export_file/download ' );
89
+ ob_end_clean ();
94
90
95
91
$ contentType = $ this ->getResponse ()->getHeader ('content-type ' );
96
92
$ contentLength = $ this ->getResponse ()->getHeader ('content-length ' );
@@ -114,33 +110,6 @@ public function testExecute(): void
114
110
);
115
111
}
116
112
117
- /**
118
- * Prepares GET request to download file.
119
- *
120
- * @param string $uri
121
- * @return void
122
- */
123
- private function prepareRequest (string $ uri ): void
124
- {
125
- $ authSession = $ this ->_objectManager ->create (Session::class);
126
- $ authSession ->setIsFirstPageAfterLogin (false );
127
- $ this ->auth ->login (
128
- TestBootstrap::ADMIN_NAME ,
129
- TestBootstrap::ADMIN_PASSWORD
130
- );
131
- $ this ->auth ->setAuthStorage ($ authSession );
132
-
133
- list ($ routeName , $ controllerName , $ actionName ) = explode ('/ ' , Download::URL );
134
- $ request = $ this ->getRequest ();
135
- $ request ->setMethod (Http::METHOD_GET )
136
- ->setRouteName ($ routeName )
137
- ->setControllerName ($ controllerName )
138
- ->setActionName ($ actionName )
139
- ->setParam (BackendUrl::SECRET_KEY_PARAM_NAME , $ this ->backendUrl ->getSecretKey ())
140
- ->setRequestUri ($ uri );
141
- $ this ->backendUrl ->turnOnSecretKey ();
142
- }
143
-
144
113
/**
145
114
* @inheritdoc
146
115
*/
0 commit comments