Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix totara unit tests #822

Merged
merged 2 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/application_trait.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ trait application_trait {
* @return string mock url to use
*/
public function get_mock_url(string $path): string {
return 'http://download.moodle.org/unittest'.$path;
return 'https://download.moodle.org/unittest'.$path;
}

// @codingStandardsIgnoreStart
Expand Down
44 changes: 18 additions & 26 deletions tests/tool_dataflows_reader_csv_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,33 +36,25 @@
*/
class tool_dataflows_reader_csv_test extends \advanced_testcase {

/** @var string|null Input path for reader to read from. */
protected $inputpath = null;

/** @var string Temp path for test files. */
protected $outputpath = '';

/**
* Set up before each test
*/
protected function setUp(): void {
parent::setUp();
$this->resetAfterTest();

$this->inputpath = null;
set_config('permitted_dirs', '/tmp', 'tool_dataflows');
}

/**
* Test csv with headers included in the file contents
*/
public function test_csv_with_headers_included_in_the_file_contents() {
[$dataflow, $steps] = $this->create_dataflow();
[$dataflow, $steps, $inputpath, $outputpath] = $this->create_dataflow();

$reader = $steps[$dataflow->steps->reader->id];
$reader->vars = Yaml::dump(['testapple' => '${{ record.apple }}']);
$reader->config = Yaml::dump([
'path' => $this->inputpath,
'path' => $inputpath,
'headers' => '',
'delimiter' => ',',
]);
Expand All @@ -78,15 +70,15 @@ public function test_csv_with_headers_included_in_the_file_contents() {
$content .= implode(',', $row);
$content .= PHP_EOL;
}
file_put_contents($this->inputpath, $content);
file_put_contents($inputpath, $content);

// Execute.
ob_start();
$engine = new engine($dataflow);
$engine->execute();
ob_get_clean();

$output = file_get_contents($this->outputpath);
$output = file_get_contents($outputpath);
$this->assertEquals(7, $engine->get_variables_root()->get('steps.reader.vars.testapple'));

$this->assertEquals($content, $output);
Expand All @@ -96,13 +88,13 @@ public function test_csv_with_headers_included_in_the_file_contents() {
* Test csv with custom headers configured
*/
public function test_csv_with_custom_headers_configured() {
[$dataflow, $steps] = $this->create_dataflow();
[$dataflow, $steps, $inputpath, $outputpath] = $this->create_dataflow();
$headers = ['first', 'second', 'third'];

$reader = $steps[$dataflow->steps->reader->id];
$reader->vars = Yaml::dump(['testsecond' => '${{ record.second }}']);
$reader->config = Yaml::dump([
'path' => $this->inputpath,
'path' => $inputpath,
'headers' => implode(',', $headers),
'delimiter' => ',',
]);
Expand All @@ -118,15 +110,15 @@ public function test_csv_with_custom_headers_configured() {
$content .= implode(',', $row);
$content .= PHP_EOL;
}
file_put_contents($this->inputpath, $content);
file_put_contents($inputpath, $content);

// Execute.
ob_start();
$engine = new engine($dataflow);
$engine->execute();
ob_get_clean();

$output = file_get_contents($this->outputpath);
$output = file_get_contents($outputpath);
$this->assertEquals(8, $engine->get_variables_root()->get('steps.reader.vars.testsecond'));

// Expected output; Add a header line.
Expand All @@ -138,7 +130,7 @@ public function test_csv_with_custom_headers_configured() {
* Tests for an invalid input stream.
*/
public function test_bad_input_stream() {
[$dataflow, $steps] = $this->create_dataflow();
[$dataflow, $steps, $inputpath, $outputpath] = $this->create_dataflow();
$path = 'path/to/nowhere';

$reader = $steps[$dataflow->steps->reader->id];
Expand Down Expand Up @@ -166,12 +158,12 @@ public function test_bad_input_stream() {
* Test csv with custom headers configured
*/
public function test_csv_with_overwrite_headers_configured() {
[$dataflow, $steps] = $this->create_dataflow();
[$dataflow, $steps, $inputpath, $outputpath] = $this->create_dataflow();
$headers = ['first', 'second', 'third'];

$reader = $steps[$dataflow->steps->reader->id];
$reader->config = Yaml::dump([
'path' => $this->inputpath,
'path' => $inputpath,
'headers' => implode(',', $headers),
'overwriteheaders' => '1',
'delimiter' => ',',
Expand All @@ -188,7 +180,7 @@ public function test_csv_with_overwrite_headers_configured() {
$content .= implode(',', $row);
$content .= PHP_EOL;
}
file_put_contents($this->inputpath, $content);
file_put_contents($inputpath, $content);

// Expected content, which has headers but first line of content is removed.
array_shift($data);
Expand All @@ -204,7 +196,7 @@ public function test_csv_with_overwrite_headers_configured() {
$engine->execute();
ob_get_clean();

$output = file_get_contents($this->outputpath);
$output = file_get_contents($outputpath);

// Expected output; Add a header line.
$this->assertEquals($expected, $output);
Expand All @@ -224,32 +216,32 @@ public function create_dataflow(): array {

$steps = [];

$this->inputpath = tempnam('', 'tool_dataflows');
$inputpath = tempnam('', 'tool_dataflows');
$reader = new step();
$reader->name = 'reader';
$reader->type = reader_csv::class;
$reader->config = Yaml::dump([
'path' => $this->inputpath,
'path' => $inputpath,
'headers' => '',
'delimiter' => ',',
]);
$dataflow->add_step($reader);
$steps[$reader->id] = $reader;

$this->outputpath = tempnam('', 'tool_dataflows');
$outputpath = tempnam('', 'tool_dataflows');
$writer = new step();
$writer->name = 'stream-writer';
$writer->type = 'tool_dataflows\local\step\writer_stream';
$writer->config = Yaml::dump([
'format' => 'csv',
'streamname' => $this->outputpath,
'streamname' => $outputpath,
]);
$writer->depends_on([$reader]);
$dataflow->add_step($writer);

$this->reader = $reader;
$this->writer = $writer;

return [$dataflow, $steps];
return [$dataflow, $steps, $inputpath, $outputpath];
}
}
Loading