Commit 774a884c authored by Nathan Lewis's avatar Nathan Lewis Committed by Aaron Wells
Browse files

Fix phpunit testing framework (Bug #1288439)



- uninstall does not remove triggers that are added by
notification/internal module, causing errors when trying to re-run
testing (when testing, all database objects are deleted then recreated,
but the triggers already exist).

- assertType is no longer available and should use assertInternalType
instead (for int value).

- there is a typo in lib/ddl.php find_key_name function which causes
problems when creating and removing the test db tables.

- there is a logic problem in lib/ddl.php uninstall_from_xmldb_file.
array_multisort is being used incorrectly and sometimes causes the
primary key to be removed before foreign keys which results in a
database error.

- the function removecolumn in lib/view.php has changed and the test is
no longer valid.

Change-Id: Ibee48e557912e09cf6533132ba771bfb2c44749f
Signed-off-by: default avatarNathan Lewis <nathan.lewis@totaralms.com>
parent 380be69c
......@@ -392,7 +392,7 @@ function find_key_name(XMLDBTable $table, XMLDBKey $key) {
global $CFG, $db;
// Do this function silently to avoid output during the install/upgrade process
$olddebug = $db->debug;
$olddbdebug = $db->debug;
$db->debug = false;
if (!table_exists($table)) {
$db->debug = $olddbdebug;
......@@ -483,7 +483,7 @@ function find_key_name(XMLDBTable $table, XMLDBKey $key) {
$constraintrec = get_records_sql_array($sql, $params);
// No constraints of the correct type on this table
if (!$constraintrec) {
$db->debug = $olddebug;
$db->debug = $olddbdebug;
return false;
}
......@@ -537,12 +537,12 @@ function find_key_name(XMLDBTable $table, XMLDBKey $key) {
}
// If they made it this far, then it's a match!
$db->debug = $olddebug;
$db->debug = $olddbdebug;
return $c->constraint_name;
}
// None matched, so return false
$db->debug = $olddebug;
$db->debug = $olddbdebug;
return false;
}
......@@ -683,11 +683,6 @@ function uninstall_from_xmldb_file($file) {
if ($tables = array_reverse($structure->getTables())) {
foreach ($tables as $table) {
if ($indexes = $table->getIndexes()) {
$sortindexes = array();
foreach ($indexes as $index) {
$sortindexes[] = find_index_name($table, $index);
}
array_multisort($indexes, SORT_DESC, $sortindexes);
foreach ($indexes as $index) {
if ($index->getName() == 'usernameuk' && is_postgres()) {
// this is a giant hack, but adodb cannot handle resolving
......@@ -702,9 +697,9 @@ function uninstall_from_xmldb_file($file) {
if ($keys = $table->getKeys()) {
$sortkeys = array();
foreach ($keys as $key) {
$sortkeys[] = find_key_name($table, $key);
$sortkeys[] = $key->type;
}
array_multisort($keys, SORT_DESC, $sortkeys);
array_multisort($sortkeys, SORT_DESC, $keys);
foreach ($keys as $key) {
if (!is_postgres() && $key->type != XMLDB_KEY_FOREIGN && $key->type != XMLDB_KEY_FOREIGN_UNIQUE) {
// Skip keys for MySQL because these will be
......
......@@ -1385,7 +1385,15 @@ function execute_sql_arr($sqlarr, $continue=true, $feedback=true) {
$status = true;
foreach($sqlarr as $sql) {
if (!execute_sql($sql)) {
try {
if (!execute_sql($sql)) {
$status = false;
if (!$continue) {
break;
}
}
}
catch (Exception $e) {
$status = false;
if (!$continue) {
break;
......@@ -1790,12 +1798,12 @@ function db_drop_trigger($name, $table) {
if (is_postgres()) {
$functionname = $name . '_function';
$triggername = $name . '_trigger';
execute_sql('DROP TRIGGER {' . $triggername . '} ON {' . $table . '}');
execute_sql('DROP FUNCTION {' . $functionname . '}()');
execute_sql('DROP TRIGGER IF EXISTS {' . $triggername . '} ON {' . $table . '}');
execute_sql('DROP FUNCTION IF EXISTS {' . $functionname . '}()');
}
else if (is_mysql()) {
$triggername = $name . '_trigger';
execute_sql('DROP TRIGGER {' . $triggername . '}');
execute_sql('DROP TRIGGER IF EXISTS {' . $triggername . '}');
}
else {
throw new SQLException("db_drop_trigger() is not implemented for your database engine");
......
......@@ -111,12 +111,28 @@ class UnitTestBootstrap {
// These constraints must be dropped manually as they cannot be
// created with xmldb due to ordering issues
if (is_postgres()) {
execute_sql('ALTER TABLE {usr} DROP CONSTRAINT {usr_pro_fk}');
execute_sql('ALTER TABLE {institution} DROP CONSTRAINT {inst_log_fk}');
try {
execute_sql('ALTER TABLE {usr} DROP CONSTRAINT {usr_pro_fk}');
}
catch (Exception $e) {
}
try {
execute_sql('ALTER TABLE {institution} DROP CONSTRAINT {inst_log_fk}');
}
catch (Exception $e) {
}
}
else {
execute_sql('ALTER TABLE {usr} DROP FOREIGN KEY {usr_pro_fk}');
execute_sql('ALTER TABLE {institution} DROP FOREIGN KEY {inst_log_fk}');
try {
execute_sql('ALTER TABLE {usr} DROP FOREIGN KEY {usr_pro_fk}');
}
catch (Exception $e) {
}
try {
execute_sql('ALTER TABLE {institution} DROP FOREIGN KEY {inst_log_fk}');
}
catch (Exception $e) {
}
}
uninstall_from_xmldb_file(get_config('docroot') . 'lib/db/install.xml');
......
......@@ -7,7 +7,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
* @copyright For copyright information on Mahara, please see the README file distributed with this software.
* @copyright (C) 2009 Penny Leach
*
*
*/
require_once(get_config('libroot') . 'view.php');
......@@ -58,7 +58,7 @@ class ViewTest extends MaharaUnitTest {
* make sure what got created makes sense
*/
public function testViewCreating() {
$this->assertType('int', (int) $this->viewid);
$this->assertInternalType('int', (int) $this->viewid);
$this->assertGreaterThan(0, $this->viewid);
// now get it again and make sure it matches
......@@ -106,12 +106,14 @@ class ViewTest extends MaharaUnitTest {
* Test that removing a column updates numcolumns
*/
public function testRemovecolumn() {
$before = $this->view->get('numcolumns');
$expect = $before > 1 ? $before - 1 : 1;
$before = $this->view->get_row_datastructure();
$this->view->removecolumn(array('column' => 1, 'row' => 1));
$this->view->removecolumn(array('column' => 1));
$after = $this->view->get_row_datastructure();
$this->assertEquals($this->view->get('numcolumns'), $expect);
$this->assertEquals(count($before), count($after));
$this->assertEquals(count($before[1]) - 1, count($after[1]));
}
/**
......@@ -140,7 +142,6 @@ class ViewTest extends MaharaUnitTest {
*/
public function tearDown() {
$this->view->delete();
$this->view->commit();
parent::tearDown();
}
}
......@@ -16,7 +16,9 @@ define('PUBLIC', 1);
// necessary since we're running in a limited scope
global $CFG, $db, $SESSION, $USER, $THEME;
ob_start();
require(dirname(dirname(dirname(dirname(__FILE__)))) . '/init.php');
ob_end_clean();
require_once(get_config('libroot') . 'ddl.php');
require_once(get_config('libroot') . 'upgrade.php');
require_once(get_config('libroot') . 'phpunit.php');
......
......@@ -45,6 +45,10 @@ class PluginNotificationInternal extends PluginNotification {
public static function postinst($prevversion) {
if ($prevversion == 0) {
// Delete the triggers first, in case they already exist.
db_drop_trigger('update_unread_insert', 'notification_internal_activity');
db_drop_trigger('update_unread_update', 'notification_internal_activity');
db_drop_trigger('update_unread_delete', 'notification_internal_activity');
// Add triggers to update user unread message count when updating
// notification_internal_activity
db_create_trigger(
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment