From 40a6b502ae87408fe74121208539647a3bd62d8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alberto=20Lara=20Hern=C3=A1ndez?= Date: Thu, 13 Jun 2019 22:34:37 +0200 Subject: [PATCH 1/2] MDL-65919 core: Skip send headers during phpunit exec in dataformats --- lib/classes/dataformat/base.php | 2 +- lib/classes/dataformat/spout_base.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/classes/dataformat/base.php b/lib/classes/dataformat/base.php index d00e46f537c..b9682b92a84 100644 --- a/lib/classes/dataformat/base.php +++ b/lib/classes/dataformat/base.php @@ -76,7 +76,7 @@ abstract class base { * Output file headers to initialise the download of the file. */ public function send_http_headers() { - if (defined('BEHAT_SITE_RUNNING')) { + if (defined('BEHAT_SITE_RUNNING') || PHPUNIT_TEST) { // For text based formats - we cannot test the output with behat if we force a file download. return; } diff --git a/lib/classes/dataformat/spout_base.php b/lib/classes/dataformat/spout_base.php index f7d8a74d9bd..55947ef67e7 100644 --- a/lib/classes/dataformat/spout_base.php +++ b/lib/classes/dataformat/spout_base.php @@ -56,7 +56,11 @@ abstract class spout_base extends \core\dataformat\base { $this->writer->setTempFolder(make_request_directory()); } $filename = $this->filename . $this->get_extension(); - $this->writer->openToBrowser($filename); + if (PHPUNIT_TEST) { + $this->writer->openToFile('php://output'); + } else { + $this->writer->openToBrowser($filename); + } // By default one sheet is always created, but we want to rename it when we call start_sheet(). $this->renamecurrentsheet = true; From b51bfaf59458ff56d87ea77e70a8ce3f631d5f8d Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Fri, 27 Sep 2019 22:02:21 +0200 Subject: [PATCH 2/2] MDL-65919 core: add test for table download --- lib/tests/tablelib_test.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/tests/tablelib_test.php b/lib/tests/tablelib_test.php index e43fdb11a38..63146696de5 100644 --- a/lib/tests/tablelib_test.php +++ b/lib/tests/tablelib_test.php @@ -618,4 +618,26 @@ class core_tablelib_testcase extends basic_testcase { unset($_GET['tifirst']); $this->assertTrue($table->can_be_reset()); } + + /** + * Test export in CSV format + */ + public function test_table_export() { + $table = new flexible_table('tablelib_test_export'); + $table->define_baseurl('/invalid.php'); + $table->define_columns(['c1', 'c2', 'c3']); + $table->define_headers(['Col1', 'Col2', 'Col3']); + + ob_start(); + $table->is_downloadable(true); + $table->is_downloading('csv'); + + $table->setup(); + $table->add_data(['column0' => 'a', 'column1' => 'b', 'column2' => 'c']); + $output = ob_get_contents(); + ob_end_clean(); + + $this->assertEquals("Col1,Col2,Col3\na,b,c\n", substr($output, 3)); + } + }