Issue #42 - Refactoring code according to Moodle Coding Style.

This commit is contained in:
Daniel Thee Roperto
2016-09-21 18:38:12 +10:00
parent 19b66629a8
commit 708622bbcd
47 changed files with 687 additions and 645 deletions

View File

@@ -14,32 +14,32 @@
// 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\cli\cliexception;
use auth_outage\cli\create;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
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 Catalyst IT
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\cli\clibase
* @covers \auth_outage\cli\cliexception
* @covers \auth_outage\local\cli\clibase
* @covers \auth_outage\local\cli\cli_exception
* @SuppressWarnings("public")
*/
class cli_test extends cli_testcase {
public function test_invalidargumentparam() {
$this->set_parameters(['--aninvalidparameter']);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
new create();
}
public function test_invalidargumentgiven() {
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
new create(['anotherinvalidparameter']);
}
@@ -51,7 +51,7 @@ class cli_test extends cli_testcase {
public function test_setreferencetime_invalid() {
$cli = new create(['start' => 0]);
$this->setExpectedException(InvalidArgumentException::class);
$this->setExpectedException(coding_exception::class);
$cli->set_referencetime(-1);
}
@@ -64,7 +64,7 @@ class cli_test extends cli_testcase {
}
public function test_exception() {
self::setExpectedException(cliexception::class, '*ERROR* An CLI exception.', 5);
throw new cliexception('An CLI exception.', 5);
self::setExpectedException(cli_exception::class, '*ERROR* An CLI exception.', 5);
throw new cli_exception('An CLI exception.', 5);
}
}

View File

@@ -14,7 +14,7 @@
// 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\cli\clibase;
use auth_outage\local\cli\clibase;
defined('MOODLE_INTERNAL') || die();
@@ -23,7 +23,7 @@ defined('MOODLE_INTERNAL') || die();
*
* @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 cli_testcase extends advanced_testcase {
@@ -35,7 +35,7 @@ class cli_testcase extends advanced_testcase {
/**
* Mocks the command line parameters.
* @param array $options Options to use as parameters.
* @param string[] $options Options to use as parameters.
*/
protected function set_parameters(array $options) {
array_unshift($options, 'cli.php');

View File

@@ -14,27 +14,27 @@
// 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\cli\cliexception;
use auth_outage\cli\create;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\create;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
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 Catalyst IT
* @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 {
public function test_noarguments() {
$cli = new create();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -47,7 +47,7 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -60,7 +60,7 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -73,7 +73,7 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -86,7 +86,7 @@ class create_test extends cli_testcase {
'title' => 'Default Title',
'description' => 'Default Description',
]);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -101,12 +101,12 @@ class create_test extends cli_testcase {
public function test_options() {
$cli = new create();
$options = $cli->generateoptions();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generateshortcuts();
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
@@ -209,7 +209,7 @@ class create_test extends cli_testcase {
$this->set_parameters([
'--onlyid',
'--start=60',
'--clone=' . $id,
'--clone='.$id,
]);
$cli = new create();
$cli->set_referencetime($now);
@@ -224,7 +224,7 @@ class create_test extends cli_testcase {
}
public function test_create_withclone_invalid() {
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->set_parameters([
'--start=60',
'--clone=-1',

View File

@@ -14,22 +14,22 @@
// 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\cli\cliexception;
use auth_outage\cli\finish;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\finish;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
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 Catalyst IT
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\cli\finish
* @covers \auth_outage\local\cli\finish
*/
class finish_test extends cli_testcase {
public function test_constructor() {
@@ -40,12 +40,12 @@ class finish_test extends cli_testcase {
public function test_options() {
$cli = new finish();
$options = $cli->generateoptions();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
$shorts = $cli->generateshortcuts();
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
@@ -61,7 +61,7 @@ class finish_test extends cli_testcase {
public function test_noarguments() {
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -75,10 +75,10 @@ class finish_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$this->set_parameters(['-id='.$id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -92,7 +92,7 @@ class finish_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$this->set_parameters(['-id='.$id]);
$cli = new finish();
$cli->set_referencetime($now);
$this->execute($cli);
@@ -102,7 +102,7 @@ class finish_test extends cli_testcase {
$this->setAdminUser();
$this->set_parameters(['-a']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -110,7 +110,7 @@ class finish_test extends cli_testcase {
$this->setAdminUser();
$this->set_parameters(['-id=theid']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -118,7 +118,7 @@ class finish_test extends cli_testcase {
$this->setAdminUser();
$this->set_parameters(['-id=99999']);
$cli = new finish();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
}

View File

@@ -14,22 +14,22 @@
// 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\cli\cliexception;
use auth_outage\cli\waitforit;
use auth_outage\models\outage;
use auth_outage\outagedb;
use auth_outage\local\cli\cli_exception;
use auth_outage\local\cli\waitforit;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
require_once('cli_testcase.php');
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 Catalyst IT
* @copyright 2016 Catalyst IT
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \auth_outage\cli\waitforit
* @covers \auth_outage\local\cli\waitforit
* @SuppressWarnings("public")
*/
class waitforit_test extends cli_testcase {
@@ -40,7 +40,7 @@ class waitforit_test extends cli_testcase {
public function test_generateoptions() {
$cli = new waitforit();
$options = $cli->generateoptions();
$options = $cli->generate_options();
foreach (array_keys($options) as $k) {
self::assertTrue(is_string($k));
}
@@ -48,8 +48,8 @@ class waitforit_test extends cli_testcase {
public function test_generateshortcuts() {
$cli = new waitforit();
$options = $cli->generateoptions();
$shorts = $cli->generateshortcuts();
$options = $cli->generate_options();
$shorts = $cli->generate_shortcuts();
foreach ($shorts as $s) {
self::assertArrayHasKey($s, $options);
}
@@ -66,21 +66,21 @@ class waitforit_test extends cli_testcase {
public function test_bothparams() {
$this->set_parameters(['--outageid=1', '--active']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$cli->execute();
}
public function test_invalidoutageid() {
$this->set_parameters(['-id=-1']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
public function test_outagenotfound() {
$this->set_parameters(['-a']);
$cli = new waitforit();
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -94,10 +94,10 @@ class waitforit_test extends cli_testcase {
'title' => 'Title',
'description' => 'Description',
]));
$this->set_parameters(['-id=' . $id]);
$this->set_parameters(['-id='.$id]);
$cli = new waitforit();
$cli->set_referencetime($now);
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
@@ -166,7 +166,7 @@ class waitforit_test extends cli_testcase {
// Pretend it is time to start, but it should get an error instead.
return $outage->starttime;
});
$this->setExpectedException(cliexception::class);
$this->setExpectedException(cli_exception::class);
$this->execute($cli);
}
}

View File

@@ -14,8 +14,8 @@
// 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\models\outage;
use auth_outage\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
@@ -61,7 +61,7 @@ class events_test extends advanced_testcase {
}
/**
* @param array $ids
* @param int[] $ids
* @depends test_save
*/
public function test_update($ids) {
@@ -90,7 +90,7 @@ class events_test extends advanced_testcase {
}
/**
* @param array $ids
* @param int[] $ids
* @depends test_update
*/
public function test_delete($ids) {

View File

@@ -14,8 +14,8 @@
// 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\infopage_controller;
use auth_outage\models\outage;
use auth_outage\local\controllers\infopage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
@@ -24,7 +24,7 @@ defined('MOODLE_INTERNAL') || die();
*
* @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\infopage_controller
*/
@@ -34,7 +34,7 @@ class infopagecontroller_test extends advanced_testcase {
if (is_file($this->get_file())) {
unlink($this->get_file());
} else {
self::fail('Invalid temp file: ' . $this->get_file());
self::fail('Invalid temp file: '.$this->get_file());
}
}
}
@@ -44,7 +44,7 @@ class infopagecontroller_test extends advanced_testcase {
* @return string Default file.
*/
public function get_file() {
return sys_get_temp_dir() . '/phpunit_authoutage.tmp';
return sys_get_temp_dir().'/phpunit_authoutage.tmp';
}
public function test_staticpage_output() {
@@ -61,13 +61,13 @@ class infopagecontroller_test extends advanced_testcase {
'title' => 'Outage Title at {{start}}',
'description' => 'This is an <b>important</b> outage, starting at {{start}}.',
]);
$info = new infopage_controller(['static' => true, 'outage' => $outage]);
$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_controller::find_outageid_from_infopage($html));
self::assertSame($outage->id, infopage::find_outageid_from_infopage($html));
}
public function test_staticpage_file() {
@@ -80,17 +80,17 @@ class infopagecontroller_test extends advanced_testcase {
'title' => 'Title',
'description' => 'Description',
]);
infopage_controller::save_static_page($outage, $this->get_file());
infopage::save_static_page($outage, $this->get_file());
self::assertFileExists($this->get_file());
$id = infopage_controller::find_outageid_from_infopage(file_get_contents($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_controller::get_defaulttemplatefile();
$file = infopage::get_defaulttemplatefile();
self::assertTrue(is_string($file));
self::assertContains('template', $file);
}

View File

@@ -14,7 +14,7 @@
// 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\models\outage;
use auth_outage\local\outage;
defined('MOODLE_INTERNAL') || die();
@@ -23,9 +23,9 @@ defined('MOODLE_INTERNAL') || die();
*
* @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\models\outage
* @covers \auth_outage\local\outage
*/
class outage_test extends basic_testcase {
public function test_constructor() {
@@ -47,7 +47,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertFalse($outage->is_ongoing($now));
@@ -57,7 +57,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertTrue($outage->is_ongoing($now));
@@ -67,7 +67,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertFalse($outage->is_ongoing($now));
}
@@ -81,7 +81,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now - (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertFalse($outage->is_active($now));
@@ -91,7 +91,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now + (1 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertTrue($outage->is_active($now));
@@ -101,7 +101,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now + (2 * 60 * 60),
'warntime' => $now - (2 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertTrue($outage->is_active($now));
@@ -111,7 +111,7 @@ class outage_test extends basic_testcase {
'stoptime' => $now + (3 * 60 * 60),
'warntime' => $now + (1 * 60 * 60),
'title' => '',
'description' => ''
'description' => '',
]);
self::assertFalse($outage->is_active($now));
}

View File

@@ -14,8 +14,8 @@
// 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\models\outage;
use auth_outage\outagedb;
use auth_outage\local\outage;
use auth_outage\local\outagedb;
defined('MOODLE_INTERNAL') || die();
@@ -24,22 +24,14 @@ defined('MOODLE_INTERNAL') || die();
*
* @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 {
/**
* Ensure DB tests run as admin.
*/
public function setUp() {
parent::setUp();
$this->setAdminUser();
}
/**
* Creates an array of ids in from the given outages array.
* @param array $outages An array of outages.
* @return array An array with the keys of the outages as values.
* @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 = [];
@@ -49,6 +41,36 @@ class outagedb_test extends advanced_testcase {
return $ids;
}
/**
* Helper function to create an outage then save it to the database.
*
* @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($now, $warning, $start, $stop, $title, $finished = null) {
return outagedb::save(new outage([
'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.',
]));
}
/**
* Ensure DB tests run as admin.
*/
public function setUp() {
parent::setUp();
$this->setAdminUser();
}
/**
* Make sure we can save and update.
*/
@@ -150,7 +172,7 @@ class outagedb_test extends advanced_testcase {
self::assertNotNull($inserted);
// Check its data.
foreach (['starttime', 'stoptime', 'warntime', 'title', 'description'] as $field) {
self::assertSame($outage->$field, $inserted->$field, 'Field ' . $field . ' does not match.');
self::assertSame($outage->$field, $inserted->$field, 'Field '.$field.' does not match.');
}
// Check generated data.
self::assertGreaterThan(0, $inserted->id);
@@ -158,11 +180,11 @@ 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::get_by_id($id);
self::assertSame('Title ID' . $id, $updated->title);
self::assertSame('Title ID'.$id, $updated->title);
self::assertSame($inserted->description, $updated->description);
// Delete it.
outagedb::delete($id);
@@ -295,28 +317,6 @@ class outagedb_test extends advanced_testcase {
self::createidarray(outagedb::get_all_ended($now)), 'Wrong past data.');
}
/**
* Helper function to create an outage then save it to the database.
*
* @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($now, $warning, $start, $stop, $title, $finished = null) {
return outagedb::save(new outage([
'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.'
]));
}
/**
* Helper function to create an outage for tests.
*
@@ -328,8 +328,8 @@ class outagedb_test extends advanced_testcase {
'starttime' => $i * 100,
'stoptime' => $i * 100 + 50,
'warntime' => $i * 60,
'title' => 'The Title ' . $i,
'description' => 'A <b>description</b> in HTML.'
'title' => 'The Title '.$i,
'description' => 'A <b>description</b> in HTML.',
]);
}
}