diff --git a/src/Forms/GridFieldQueuedExportButton.php b/src/Forms/GridFieldQueuedExportButton.php index 145d4f1..907256f 100644 --- a/src/Forms/GridFieldQueuedExportButton.php +++ b/src/Forms/GridFieldQueuedExportButton.php @@ -195,7 +195,7 @@ public function checkExport($gridField, $request = null) ]); if ($job->JobStatus == QueuedJob::STATUS_COMPLETE) { - if (file_exists($this->getExportPath($id))) { + if (file_exists($this->getExportPath($id) ?? '')) { $data->DownloadLink = $gridField->Link('/export_download/' . $job->Signature); } else { $data->ErrorMessage = _t( @@ -253,10 +253,10 @@ public function downloadExport($gridField, $request = null) $servedName = "export-$now.csv"; $path = $this->getExportPath($id); - $content = file_get_contents($path); + $content = file_get_contents($path ?? ''); - unlink($path); - rmdir(dirname($path)); + unlink($path ?? ''); + rmdir(dirname($path ?? '')); $response = HTTPRequest::send_file($content, $servedName, 'text/csv'); $response->addHeader('Set-Cookie', 'downloaded_' . $id . '=true; Path=/'); diff --git a/src/Jobs/GenerateCSVJob.php b/src/Jobs/GenerateCSVJob.php index 9cbb9dd..c44aa9d 100644 --- a/src/Jobs/GenerateCSVJob.php +++ b/src/Jobs/GenerateCSVJob.php @@ -139,11 +139,11 @@ public function setSession($session) { // None of the gridfield actions are needed, and they make the stored session bigger, so pull // them out. - $actionkeys = array_filter(array_keys($session), function ($i) { - return strpos($i, 'gf_') === 0; + $actionkeys = array_filter(array_keys($session ?? []), function ($i) { + return strpos($i ?? '', 'gf_') === 0; }); - $session = array_diff_key($session, array_flip($actionkeys)); + $session = array_diff_key($session ?? [], array_flip($actionkeys ?? [])); // This causes problems with logins unset($session['HTTP_USER_AGENT']); @@ -176,7 +176,7 @@ public function setIncludeHeader($includeHeader) protected function makeDir($path) { - if (!is_dir($path)) { + if (!is_dir($path ?? '')) { // whether to use 'chmod' to override 'mkdir' perms which obey umask $ignore_umask = $this->config()->get('ignore_umask'); @@ -189,14 +189,14 @@ protected function makeDir($path) } // convert from octal to decimal for mkdir - $permission_mode = octdec($permission_mode); + $permission_mode = octdec($permission_mode ?? ''); // make dir with perms that obey the executing user's umask - mkdir($path, $permission_mode, true); + mkdir($path ?? '', $permission_mode ?? 0, true); // override perms to ignore user's umask? if ($ignore_umask) { - chmod($path, $permission_mode); + chmod($path ?? '', $permission_mode ?? 0); } } } @@ -234,7 +234,7 @@ protected function getCSVWriter() $csvWriter->addFormatter(function (array $row) { foreach ($row as &$item) { // [SS-2017-007] Sanitise XLS executable column values with a leading tab - if (preg_match('/^[-@=+].*/', $item)) { + if (preg_match('/^[-@=+].*/', $item ?? '')) { $item = "\t" . $item; } } @@ -284,7 +284,7 @@ protected function getGridField() $queryParams = [$actionKey => $actionValue, 'SecurityID' => $token]; // Get the filters and assign to the url as a get parameter - if (is_array($this->Filters) && count($this->Filters) > 0) { + if (is_array($this->Filters) && count($this->Filters ?? []) > 0) { foreach ($this->Filters as $filter => $value) { $queryParams['filters'][$filter] = $value; } @@ -292,7 +292,7 @@ protected function getGridField() $url = Controller::join_links( $this->GridFieldURL, - '?' . http_build_query($queryParams) + '?' . http_build_query($queryParams ?? []) ); // Restore into the current session the user the job is exporting as @@ -330,7 +330,7 @@ protected function outputHeader($gridField, $columns) // determine the CSV headers. If a field is callable (e.g. anonymous function) then use the // source name as the header instead foreach ($columns as $columnSource => $columnHeader) { - if (is_array($columnHeader) && array_key_exists('title', $columnHeader)) { + if (is_array($columnHeader) && array_key_exists('title', $columnHeader ?? [])) { $headers[] = $columnHeader['title']; } else { $headers[] = (!is_string($columnHeader) && is_callable($columnHeader)) ? $columnSource : $columnHeader; @@ -447,7 +447,7 @@ public function process() // Check to see if we need to wait for some time for asset synchronisation to complete $sleepTime = (int) $this->config()->get('sync_sleep_seconds'); if ($sleepTime > 0) { - sleep($sleepTime); + sleep($sleepTime ?? 0); } $this->isComplete = true; diff --git a/tests/GenerateCSVJobTest.php b/tests/GenerateCSVJobTest.php index c5dd060..35f9329 100644 --- a/tests/GenerateCSVJobTest.php +++ b/tests/GenerateCSVJobTest.php @@ -35,7 +35,7 @@ protected function setUp(): void protected function tearDown(): void { foreach ($this->paths as $path) { - Filesystem::removeFolder(dirname($path)); + Filesystem::removeFolder(dirname($path ?? '')); } parent::tearDown(); } @@ -70,9 +70,9 @@ public function testGenerateExport() '"Record 3","

""Record 3"" Body

","2015-01-03 23:34:01"', '', ]; - $actual = file_get_contents($path); + $actual = file_get_contents($path ?? ''); // Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3 - $this->assertStringContainsString('title,content,"publish on"', strtolower($actual)); + $this->assertStringContainsString('title,content,"publish on"', strtolower($actual ?? '')); $this->assertStringContainsString(implode("\r\n", $expected), $actual); } @@ -118,9 +118,9 @@ public function testGenerateExportOverMultipleSteps() '"Record 3","

""Record 3"" Body

","2015-01-03 23:34:01"', '', ]; - $actual = file_get_contents($path); + $actual = file_get_contents($path ?? ''); // Note: strtolower() is for case insensitive comparison, since field label casing changed in SS 4.3 - $this->assertStringContainsString('title,content,"publish on"', strtolower($actual)); + $this->assertStringContainsString('title,content,"publish on"', strtolower($actual ?? '')); $this->assertStringContainsString(implode("\r\n", $expected), $actual); }