Merge branch 'master' into issue19-behat

This commit is contained in:
Daniel Thee Roperto
2016-09-23 19:33:42 +10:00
63 changed files with 5237 additions and 771 deletions

82
tests/cli/cli_test.php Normal file
View File

@@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI base and exception class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\clibase
* @covers \auth_outage\local\cli\cli_exception
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 2
*/
public function test_invalidargumentparam() {
$this->set_parameters(['--aninvalidparameter']);
new create();
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 2
*/
public function test_invalidargumentgiven() {
new create(['anotherinvalidparameter']);
}
public function test_setreferencetime() {
$cli = new create(['start' => 0]);
$cli->set_referencetime(1);
$cli->set_referencetime(60 * 60 * 24 * 7);
}
/**
* @expectedException coding_exception
*/
public function test_setreferencetime_invalid() {
$this->set_parameters(['--start=60']);
$cli = new create();
$cli->set_referencetime(-1);
}
public function test_help() {
$this->set_parameters(['-h']);
$cli = new create();
$output = $this->execute($cli);
self::assertContains('-h', $output);
self::assertContains('--help', $output);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 1
*/
public function test_exception() {
throw new cli_exception('An CLI exception.');
}
}

View File

@@ -0,0 +1,61 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\local\cli\clibase;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on CLIs.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cli_testcase extends advanced_testcase {
public function setUp() {
$this->resetAfterTest(true);
$this->set_parameters([]);
parent::setUp();
}
/**
* Mocks the command line parameters.
* @param string[] $options Options to use as parameters.
*/
protected function set_parameters(array $options) {
array_unshift($options, 'cli.php');
$_SERVER['argv'] = $options;
$_SERVER['argc'] = count($options);
}
/**
* Executes the CLI.
* @param clibase $cli CLI to execute.
* @return string The output text.
*/
protected function execute(clibase $cli) {
ob_start();
try {
$cli->execute();
$text = ob_get_contents();
return $text;
} finally {
ob_end_clean();
}
}
}

276
tests/cli/create_test.php Normal file
View File

@@ -0,0 +1,276 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI create class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @SuppressWarnings("public") Allow this test to have as many tests as necessary.
*/
class create_test extends cli_testcase {
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 4
*/
public function test_noarguments() {
$cli = new create();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_notanumber() {
$cli = new create(['start' => 'some day']);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_negative() {
$cli = new create(['start' => -1]);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_emptystring() {
$cli = new create(['start' => 0, 'title' => '']);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidparam_notastring() {
$cli = new create(['start' => 0, 'title' => true]);
$cli->set_defaults([
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->execute($cli);
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new create();
$output = $this->execute($cli);
self::assertContains('Creates', $output);
self::assertContains('--help', $output);
}
public function test_options() {
$cli = new create();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_create_withoptions() {
$this->set_parameters([
'--autostart=true',
'--warn=10',
'--start=0',
'--duration=30',
'--title=A Title',
'--description=A Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$text = $this->execute($cli);
self::assertContains('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->starttime);
self::assertSame(10, $outage->get_warning_duration());
self::assertSame(30, $outage->get_duration_planned());
self::assertNull($outage->finished);
self::assertSame('A Title', $outage->title);
self::assertSame('A Description', $outage->description);
}
public function test_create_onlyid() {
$this->set_parameters([
'--onlyid',
'--autostart=N',
'--warn=10',
'--start=0',
'--duration=30',
'--title=Title',
'--description=Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$id = $this->execute($cli);
// Check if the id contains is only a number (parameter onlyid).
$id = trim($id);
self::assertTrue(is_number($id));
$id = (int)$id;
// Check creted outage.
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->starttime);
self::assertSame($outage->starttime - 10, $outage->warntime);
self::assertSame($outage->starttime + 30, $outage->stoptime);
self::assertNull($outage->finished);
self::assertSame('Title', $outage->title);
self::assertSame('Description', $outage->description);
}
public function test_create_withdefaults() {
$this->set_parameters([
'--warn=100',
'--start=50',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$cli->set_defaults([
'autostart' => false,
'warn' => 50,
'start' => 200,
'duration' => 300,
'title' => 'Default Title',
'description' => 'Default Description',
]);
$text = $this->execute($cli);
self::assertContains('created', $text);
// Check creted outage.
list(, $id) = explode(':', $text);
$id = (int)$id;
$outage = outagedb::get_by_id($id);
self::assertSame($now + 50, $outage->starttime, 'Wrong starttime.');
self::assertSame($outage->starttime - 100, $outage->warntime, 'Wrong warntime.');
self::assertSame($outage->starttime + 300, $outage->stoptime, 'Wrong stoptime.');
self::assertNull($outage->finished);
self::assertSame('Default Title', $outage->title);
self::assertSame('Default Description', $outage->description);
}
public function test_create_withclone() {
self::setAdminUser();
$now = time();
// Create the outage to clone.
$original = new outage([
'autostart' => false,
'warntime' => $now - 120,
'starttime' => $now,
'stoptime' => $now + 120,
'title' => 'Title',
'description' => 'Description',
]);
$id = outagedb::save($original);
// Clone it using CLI.
$this->set_parameters([
'--onlyid',
'--start=60',
'--clone='.$id,
]);
$cli = new create();
$cli->set_referencetime($now);
$id = trim($this->execute($cli));
// Check cloned data.
$cloned = outagedb::get_by_id((int)$id);
self::assertSame($now + 60, $cloned->starttime);
self::assertSame($original->get_warning_duration(), $cloned->get_warning_duration());
self::assertSame($original->get_duration_planned(), $cloned->get_duration_planned());
self::assertSame($original->title, $cloned->title);
self::assertSame($original->description, $cloned->description);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_create_withclone_invalid() {
$this->set_parameters([
'--start=60',
'--clone=-1',
]);
$cli = new create();
$this->execute($cli);
}
public function test_create_withblock() {
// Not an extensive test in the blocking API, cliwaitforit tests should cover them deeper.
$this->set_parameters([
'--autostart=N',
'--block',
'--warn=60',
'--start=0',
'--duration=600',
'--title=Title',
'--description=Description',
]);
$now = time();
$cli = new create();
$cli->set_referencetime($now);
$text = $this->execute($cli);
self::assertContains('created', $text);
self::assertContains('started', $text);
}
}

141
tests/cli/finish_test.php Normal file
View File

@@ -0,0 +1,141 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\finish;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI finish class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\finish
*/
class finish_test extends cli_testcase {
public function test_constructor() {
$cli = new finish();
self::assertNotNull($cli);
}
public function test_options() {
$cli = new finish();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new finish();
$text = $this->execute($cli);
self::assertContains('Finishes', $text);
self::assertContains('--help', $text);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 4
*/
public function test_noarguments() {
$cli = new finish();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 5
*/
public function test_endedoutage() {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);
}
public function test_finish() {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now + 100,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_activenotfound() {
self::setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidid() {
self::setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_idnotfound() {
self::setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->execute($cli);
}
}

View File

@@ -0,0 +1,190 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\cli\waitforit;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
require_once(__DIR__.'/cli_testcase.php');
/**
* Tests performed on CLI waitforit class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\cli\waitforit
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {
public function test_constructor() {
$cli = new waitforit();
self::assertNotNull($cli);
}
public function test_generateoptions() {
$cli = new waitforit();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
}
public function test_generateshortcuts() {
$cli = new waitforit();
$options = $cli->generate_options();
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
}
public function test_help() {
$this->set_parameters(['--help']);
$cli = new waitforit();
$text = $this->execute($cli);
self::assertContains('Waits', $text);
self::assertContains('--help', $text);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_bothparams() {
$this->set_parameters(['--outageid=1', '--active']);
$cli = new waitforit();
$cli->execute();
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 3
*/
public function test_invalidoutageid() {
$this->set_parameters(['-id=-1']);
$cli = new waitforit();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 6
*/
public function test_outagenotfound() {
$this->set_parameters(['-a']);
$cli = new waitforit();
$this->execute($cli);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 5
*/
public function test_endedoutage() {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 200,
'starttime' => $now - 100,
'stoptime' => $now - 50,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id='.$id]);
$cli = new waitforit();
$cli->set_referencetime($now);
$this->execute($cli);
}
public function test_activeverbose() {
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 10,
'starttime' => $now + 1,
'stoptime' => $now + 10,
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active']);
$cli = new waitforit();
$cli->set_referencetime($now);
$output = $this->execute($cli);
self::assertContains('Verbose mode', $output);
self::assertContains('starting in 1 sec', $output);
self::assertContains('started', $output);
}
public function test_countdown() {
self::setAdminUser();
$now = time();
outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + 45,
'stoptime' => $now + (60 * 60),
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function ($sleep) use (&$now) {
$now += $sleep;
return $now;
});
$output = $this->execute($cli);
self::assertContains("starting in 45", $output);
self::assertContains("sleep 30 second", $output);
self::assertContains("starting in 15", $output);
self::assertContains("sleep 15 second", $output);
self::assertContains("started!", $output);
}
/**
* @expectedException auth_outage\local\cli\cli_exception
* @expectedExceptionCode 7
*/
public function test_outagechanged() {
self::setAdminUser();
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now,
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (60 * 60),
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-v', '--active', '--sleep=30']);
$cli = new waitforit();
$cli->set_referencetime($now);
$cli->set_sleepcallback(function () use ($id) {
// Change outage when not expected to.
$outage = outagedb::get_by_id($id);
$outage->title = 'New title!';
outagedb::save($outage);
// Pretend it is time to start, but it should get an error instead.
return $outage->starttime;
});
$this->execute($cli);
}
}

116
tests/events_test.php Normal file
View File

@@ -0,0 +1,116 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\event\outage_created
* @covers \auth_outage\event\outage_updated
* @covers \auth_outage\event\outage_deleted
*/
class events_test extends advanced_testcase {
public function test_save() {
global $DB;
self::setAdminUser();
$this->resetAfterTest(false);
// Save new outage.
$now = time();
$id = outagedb::save(new outage([
'autostart' => false,
'warntime' => $now - 60,
'starttime' => 60,
'stoptime' => 120,
'title' => 'Title',
'description' => 'Description',
]));
// Check existance.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :outageid)",
['outageid' => $id],
'id',
IGNORE_MISSING
);
self::assertTrue(is_object($event));
// Another test will use it.
return [$id, $event->id];
}
/**
* @param int[] $ids
* @depends test_save
*/
public function test_update($ids) {
global $DB;
self::setAdminUser();
$this->resetAfterTest(false);
list($idoutage, $idevent) = $ids;
$outage = outagedb::get_by_id($idoutage);
$outage->starttime += 10;
outagedb::save($outage);
// Should still exist.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :idoutage)",
['idoutage' => $idoutage],
'id',
IGNORE_MISSING
);
self::assertTrue(is_object($event));
self::assertSame($idevent, $event->id);
return $ids;
}
/**
* @param int[] $ids
* @depends test_update
*/
public function test_delete($ids) {
global $DB;
self::setAdminUser();
$this->resetAfterTest(true);
list($idoutage, $idevent) = $ids;
outagedb::delete($idoutage);
// Should not exist.
$event = $DB->get_record_select(
'event',
"(eventtype = 'auth_outage' AND instance = :idoutage) OR (id = :idevent)",
['idoutage' => $idoutage, 'idevent' => $idevent],
'id',
IGNORE_MISSING
);
self::assertFalse($event);
}
}

View File

@@ -0,0 +1,97 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on infopage_controller class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\infopage_controller
*/
class infopagecontroller_test extends advanced_testcase {
public function setUp() {
if (file_exists($this->get_file())) {
if (is_file($this->get_file())) {
unlink($this->get_file());
} else {
self::fail('Invalid temp file: '.$this->get_file());
}
}
}
/**
* Return a temporary file name to use for this test.
* @return string Default file.
*/
public function get_file() {
return sys_get_temp_dir().'/phpunit_authoutage.tmp';
}
public function test_staticpage_output() {
global $PAGE;
$this->resetAfterTest(true);
$PAGE->set_context(context_system::instance());
$now = time();
$outage = new outage([
'id' => 1,
'starttime' => $now + (60 * 60),
'warntime' => $now - (60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'title' => 'Outage Title at {{start}}',
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
]);
$info = new infopage(['static' => true, 'outage' => $outage]);
$html = $info->get_output();
self::assertContains('<!DOCTYPE html>', $html);
self::assertContains('</html>', $html);
self::assertContains($outage->get_title(), $html);
self::assertContains($outage->get_description(), $html);
self::assertSame($outage->id, infopage::find_outageid_from_infopage($html));
}
public function test_staticpage_file() {
$now = time();
$outage = new outage([
'id' => 1,
'warntime' => $now - 100,
'starttime' => $now + 100,
'stoptime' => $now + 200,
'title' => 'Title',
'description' => 'Description',
]);
infopage::save_static_page($outage, $this->get_file());
self::assertFileExists($this->get_file());
$id = infopage::find_outageid_from_infopage(file_get_contents($this->get_file()));
self::assertSame($outage->id, $id);
unlink($this->get_file());
}
public function test_getdefaulttemplatefile() {
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}
}

View File

@@ -14,22 +14,20 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\local\outage
*/
use auth_outage\models\outage;
defined('MOODLE_INTERNAL') || die();
class outage_test extends basic_testcase
{
class outage_test extends basic_testcase {
public function test_constructor() {
$outage = new outage();
// Very important, this should never change.
@@ -39,4 +37,148 @@ class outage_test extends basic_testcase
self::assertNull($v);
}
}
public function test_isongoing() {
$now = time();
// In the past.
$outage = new outage([
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertFalse($outage->is_ongoing($now));
// In the present (ongoing).
$outage = new outage([
'starttime' => $now - (1 * 60 * 60),
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertTrue($outage->is_ongoing($now));
// In the future.
$outage = new outage([
'starttime' => $now + (1 * 60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertFalse($outage->is_ongoing($now));
}
public function test_isactive() {
$now = time();
// In the past.
$outage = new outage([
'starttime' => $now - (3 * 60 * 60),
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertFalse($outage->is_active($now));
// In the present (ongoing).
$outage = new outage([
'starttime' => $now - (1 * 60 * 60),
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertTrue($outage->is_active($now));
// In the future (warning).
$outage = new outage([
'starttime' => $now + (1 * 60 * 60),
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertTrue($outage->is_active($now));
// In the future (not warning).
$outage = new outage([
'starttime' => $now + (2 * 60 * 60),
'stoptime' => $now + (3 * 60 * 60),
'warntime' => $now + (1 * 60 * 60),
'title' => '',
'description' => '',
]);
self::assertFalse($outage->is_active($now));
}
public function test_stages() {
$now = time();
$outage = new outage([
'warntime' => $now + 10,
'starttime' => $now + 20,
'stoptime' => $now + 30,
'title' => 'Outage Waiting',
]);
self::assertSame(outage::STAGE_WAITING, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
$outage = new outage([
'warntime' => $now - 10,
'starttime' => $now + 20,
'stoptime' => $now + 30,
'title' => 'Outage Warning',
]);
self::assertSame(outage::STAGE_WARNING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
$outage = new outage([
'warntime' => $now - 20,
'starttime' => $now - 10,
'stoptime' => $now + 30,
'title' => 'Outage Ongoing',
]);
self::assertSame(outage::STAGE_ONGOING, $outage->get_stage($now));
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
$outage = new outage([
'warntime' => $now - 50,
'starttime' => $now - 40,
'stoptime' => $now - 30,
'title' => 'Outage Stopped',
]);
self::assertSame(outage::STAGE_STOPPED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
$outage = new outage([
'warntime' => $now - 50,
'starttime' => $now - 40,
'finished' => $now - 30,
'stoptime' => $now - 20,
'title' => 'Outage Finished before Stop',
]);
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
$outage = new outage([
'warntime' => $now - 50,
'starttime' => $now - 40,
'stoptime' => $now - 30,
'finished' => $now - 20,
'title' => 'Outage Finished after Stop',
]);
self::assertSame(outage::STAGE_FINISHED, $outage->get_stage($now));
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
}
}

View File

@@ -14,23 +14,65 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
use auth_outage\dml\outagedb;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
/**
* Tests performed on outage class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class outagedb_test extends advanced_testcase {
/**
* Creates an array of ids in from the given outages array.
* @param outage[] $outages An array of outages.
* @return int[] An array with the keys of the outages as values.
*/
private static function createidarray(array $outages) {
$ids = [];
foreach ($outages as $outage) {
$ids[] = $outage->id;
}
return $ids;
}
use auth_outage\models\outage;
use auth_outage\outagedb;
/**
* Helper function to create an outage then save it to the database.
*
* @param bool $autostart If outage should automatically start.
* @param int $now Timestamp for now, such as time().
* @param int $warning In how many hours the warning starts. Can be negative.
* @param int $start In how many hours this outage starts. Can be negative.
* @param int $stop In how many hours this outage finishes. Can be negative.
* @param string $title Title for the outage.
* @param int|null $finished In how many hours this outage is marked as finished. Can be negative or null.
* @return int Id the of created outage.
*/
private static function saveoutage($autostart, $now, $warning, $start, $stop, $title, $finished = null) {
return outagedb::save(new outage([
'autostart' => $autostart,
'warntime' => $now + ($warning * 60 * 60),
'starttime' => $now + ($start * 60 * 60),
'stoptime' => $now + ($stop * 60 * 60),
'finished' => is_null($finished) ? null : ($now + ($finished * 60 * 60)),
'title' => $title,
'description' => 'Test Outage Description.',
]));
}
defined('MOODLE_INTERNAL') || die();
/**
* Ensure DB tests run as admin.
*/
public function setUp() {
parent::setUp();
$this->setAdminUser();
}
class outagedb_test extends advanced_testcase
{
/**
* Make sure we can save and update.
*/
@@ -44,6 +86,21 @@ class outagedb_test extends advanced_testcase
outagedb::save($outage);
}
public function test_saved_fields() {
$this->resetAfterTest(true);
for ($i = 0; $i < 4; $i++) {
$expected = $this->createoutage($i);
$expected->id = outagedb::save($expected);
$actual = outagedb::get_by_id($expected->id);
// Ignore the following fields.
$expected->lastmodified = $actual->lastmodified;
$expected->createdby = $actual->createdby;
$expected->modifiedby = $actual->modifiedby;
// Check if fields are the same.
self::assertEquals($expected, $actual, 'Failed for $i='.$i);
}
}
/**
* Make sure we can get existing entries and null if not found.
*/
@@ -52,12 +109,12 @@ class outagedb_test extends advanced_testcase
// Create something.
$id = outagedb::save($this->createoutage(1));
// Get should work.
$outage = outagedb::getbyid($id);
$outage = outagedb::get_by_id($id);
self::assertNotNull($outage);
// Delete it.
outagedb::delete($id);
// Get should be null.
$outage = outagedb::getbyid($id);
$outage = outagedb::get_by_id($id);
self::assertNull($outage);
}
@@ -71,7 +128,27 @@ class outagedb_test extends advanced_testcase
// Delete it.
outagedb::delete($id);
// Should not exist anymore.
self::assertNull(outagedb::getbyid($id));
self::assertNull(outagedb::get_by_id($id));
}
/**
* Make sure we can finish outages.
*/
public function test_finish() {
$now = time();
$this->resetAfterTest(true);
// Create it.
$id = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
$outage = outagedb::get_by_id($id);
self::assertTrue($outage->is_active($now));
self::assertTrue($outage->is_ongoing($now));
self::assertSame(null, $outage->finished);
// Finish it.
outagedb::finish($id, $now);
$outage = outagedb::get_by_id($id);
self::assertSame($now, $outage->finished);
self::assertFalse($outage->is_active($now));
self::assertFalse($outage->is_ongoing($now));
}
/**
@@ -81,21 +158,20 @@ class outagedb_test extends advanced_testcase
$this->resetAfterTest(true);
$amount = 10;
// Should start empty.
$outages = outagedb::getall();
$outages = outagedb::get_all();
self::assertSame([], $outages);
// Create some stuff outages.
for ($i = 0; $i < $amount; $i++) {
outagedb::save($this->createoutage($i));
}
// Count entries created.
self::assertSame($amount, count(outagedb::getall()));
self::assertSame($amount, count(outagedb::get_all()));
}
/**
* Perform some tests on the data itself, checking values after inserted and updated.
*/
public function test_basiccrud() {
return;
$this->resetAfterTest(true);
// Create some outages.
@@ -109,11 +185,11 @@ class outagedb_test extends advanced_testcase
// With all created outages.
foreach ($outages as $id => $outage) {
// Get it.
$inserted = outagedb::getbyid($id);
$inserted = outagedb::get_by_id($id);
self::assertNotNull($inserted);
// Check its data.
foreach (['starttime', 'stoptime', 'warningduration', 'title', 'description'] as $field) {
self::assertSame($outage->$field, $inserted->$field, 'Field ' . $field . ' does not match.');
foreach (['starttime', 'stoptime', 'warntime', 'title', 'description'] as $field) {
self::assertSame($outage->$field, $inserted->$field, 'Field '.$field.' does not match.');
}
// Check generated data.
self::assertGreaterThan(0, $inserted->id);
@@ -121,19 +197,138 @@ class outagedb_test extends advanced_testcase
self::assertNotNull($inserted->createdby);
self::assertNotNull($inserted->modifiedby);
// Change it.
$inserted->title = 'Title ID' . $id;
$inserted->title = 'Title ID'.$id;
outagedb::save($inserted);
// Get it again and check data.
$updated = outagedb::getbyid($id);
self::assertSame('Title ID' . $id, $updated->title);
$updated = outagedb::get_by_id($id);
self::assertSame('Title ID'.$id, $updated->title);
self::assertSame($inserted->description, $updated->description);
// Delete it.
outagedb::delete($id);
$deleted = outagedb::getbyid($id);
$deleted = outagedb::get_by_id($id);
self::assertNull($deleted);
}
}
public function test_getactive() {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertNull(outagedb::get_active($now), 'There should be no active outage at this point.');
self::saveoutage(false, $now, 1, 2, 3, 'An outage that starts in the future and is not in warning period.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
self::saveoutage(false, $now, -3, -2, -1, 'An outage that is already in the past.');
self::assertNull(outagedb::get_active($now), 'No active outages yet.');
$activeid = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -1, 2, 3,
'Another outage in warning period, but ignored as it starts after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -3, -2, 2, 'An finished outage.', -1);
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
$activeid = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage but ignored because it started after the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
self::saveoutage(false, $now, -3, -2, 1,
'Another ongoing outage starting at the same time, but ignored as it stops before the previous one.');
self::assertSame($activeid, outagedb::get_active($now)->id, 'Wrong active outage picked.');
}
public function test_getallunended() {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_unended($now), 'There should be no future outages at this point.');
self::saveoutage(false, $now, -3, -2, -1, 'A past outage.');
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([], outagedb::get_all_unended($now), 'No future outages yet.');
$id1 = self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id2 = self::saveoutage(false, $now, 1, 4, 5, 'Another future outage.');
self::assertEquals([$id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id3 = self::saveoutage(false, $now, 1, 3, 5, 'Yet another future outage.');
self::assertEquals([$id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id4 = self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id5 = self::saveoutage(false, $now, -1, 2, 3, 'Another outage in warning period.');
self::assertEquals([$id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id6 = self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([$id6, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id7 = self::saveoutage(false, $now, -3, -1, 1, 'Another ongoing outage.');
self::assertEquals([$id6, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
$id8 = self::saveoutage(false, $now, -3, -2, 1, 'Yet another ongoing outage.');
self::assertEquals([$id6, $id8, $id7, $id4, $id5, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_unended($now)), 'Wrong future data.');
}
public function test_getallended() {
$this->resetAfterTest(true);
// Have a consistent time for now (no seconds variation), helps debugging.
$now = time();
self::assertEquals([], outagedb::get_all(), 'Ensure there are no other outages that can affect the test.');
self::assertEquals([], outagedb::get_all_ended($now), 'There should be no future outages at this point.');
self::saveoutage(false, $now, -2, 1, 2, 'An outage in warning period.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage(false, $now, -3, -2, 2, 'An ongoing outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
self::saveoutage(false, $now, 2, 3, 4, 'A future outage.');
self::assertEquals([], outagedb::get_all_ended($now), 'No past outages yet.');
$id1 = self::saveoutage(false, $now, -8, -6, -4, 'A past outage.');
self::assertEquals([$id1],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id2 = self::saveoutage(false, $now, -8, -7, -5, 'Another past outage.');
self::assertEquals([$id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id3 = self::saveoutage(false, $now, -8, -5, -3, 'Yet another past outage.');
self::assertEquals([$id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
$id4 = self::saveoutage(false, $now, -3, -2, 2, 'A finished outage.', -1);
self::assertEquals([$id4, $id3, $id1, $id2],
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
}
/**
* Helper function to create an outage for tests.
*
@@ -142,11 +337,12 @@ class outagedb_test extends advanced_testcase
*/
private function createoutage($i) {
return new outage([
'autostart' => ($i % 2 == 0),
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warningduration' => $i * 60,
'title' => 'The Title ' . $i,
'description' => 'A <b>description</b> in HTML.'
'warntime' => $i * 60,
'title' => 'The Title '.$i,
'description' => 'A <b>description</b> in HTML.',
]);
}
}

View File

@@ -1,52 +0,0 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Tests performed on outageutils class.
*
* @package auth_outage
* @author Daniel Thee Roperto <daniel.roperto@catalyst-au.net>
* @copyright Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use auth_outage\outagelib;
defined('MOODLE_INTERNAL') || die();
class outagelib_test extends basic_testcase
{
public function test_data2object() {
// Using object data, no new fields.
$obj = new stdClass();
$obj->foo = 'bar';
$obj->number = 42;
$data = new stdClass();
$data->foo = 'not bar';
outagelib::data2object($data, $obj);
self::assertEquals(get_object_vars($obj), ['foo' => 'not bar', 'number' => 42], 'Invalid result.');
self::assertEquals(get_object_vars($data), ['foo' => 'not bar'], 'Data should not change.');
// Using array data, with new fields.
$obj = new stdClass();
$obj->foo = 'bar';
$obj->number = 42;
$data = ['foo' => 'foobar', 'flag' => false];
outagelib::data2object($data, $obj);
self::assertEquals(get_object_vars($obj), ['foo' => 'foobar', 'number' => 42], 'Invalid result.');
}
}

31
tests/phpunit.xml Normal file
View File

@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/phpunit/phpunit.xsd"
bootstrap="../../../lib/phpunit/bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
backupGlobals="false"
backupStaticAttributes="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
printerClass="Hint_ResultPrinter"
testSuiteLoaderClass="phpunit_autoloader"
>
<testsuites>
<testsuite name="auth_outage_testsuite">
<directory suffix="_test.php">./</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../classes</directory>
</whitelist>
</filter>
</phpunit>