Moodle  2.2.1
http://www.collinsharper.com
UnitTestCaseUsingDatabase Class Reference
Inheritance diagram for UnitTestCaseUsingDatabase:
Collaboration diagram for UnitTestCaseUsingDatabase:

Public Member Functions

 __construct ($label=false)
 tearDown ()
 __destruct ()

Protected Member Functions

 switch_to_test_db ()
 revert_to_real_db ()
 switch_to_test_cfg ()
 revert_to_real_cfg ()
 switch_global_user_id ($userid)
 revert_global_user_id ()
 create_system_context_record ()
 create_test_table ($tablename, $installxmlfile)
 create_test_tables ($tablenames, $installxmlfile)
 drop_test_table ($tablename)
 drop_test_tables ($tablenames)
 load_test_data ($table, array $cols, array $data)
 delete_test_data ($table, array $rows)

Protected Attributes

 $testdb
 $testcfg

Detailed Description

Definition at line 636 of file simpletestlib.php.


Constructor & Destructor Documentation

__construct ( label = false)

Definition at line 645 of file simpletestlib.php.

Here is the call graph for this function:

Reimplemented in grade_category_test.

Definition at line 837 of file simpletestlib.php.


Member Function Documentation

Recreates the system context record in the 'context' table

Once we have switched to test db, if we have recreated the context table and it's empty, it may be necessary to manually create the system context record if unittests are going to play with contexts.

This is needed because the context_system::instance() method is exceptional and always requires the record to exist, never creating it :-( No problem for other contexts.

Altenatively one complete install can be done, like {

See also:
accesslib_test::test_everything_in_accesslib} does, but that's to much for some tests not requiring all the roles/caps/friends to be present.

Ideally some day we'll move a lot of these UnitTests to a complete cloned installation with real data to play with. That day this won't be necessary anymore.

Definition at line 769 of file simpletestlib.php.

Here is the caller graph for this function:

create_test_table ( tablename,
installxmlfile 
) [protected]

Create a test table just like a real one, getting getting the definition from the specified install.xml file.

Parameters:
string$tablenamethe name of the test table.
string$installxmlfilethe install.xml file in which this table is defined. $CFG->dirroot . '/' will be prepended, and '/db/install.xml' appended, so you need only specify, for example, 'mod/quiz'.

Definition at line 850 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

create_test_tables ( tablenames,
installxmlfile 
) [protected]

Convenience method for calling create_test_table repeatedly.

Parameters:
array$tablenamesan array of table names.
string$installxmlfilethe install.xml file in which this table is defined. $CFG->dirroot . '/' will be prepended, and '/db/install.xml' appended, so you need only specify, for example, 'mod/quiz'.

Definition at line 872 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

delete_test_data ( table,
array rows 
) [protected]

Clean up data loaded with load_test_data. The call corresponding to the example load above would be:

$this->delete_test_data('config_plugins', $config);

Parameters:
string$tablethe table name.
array$rowsthe rows to delete. Actually, only $rows[$key]->id is used.

Definition at line 940 of file simpletestlib.php.

drop_test_table ( tablename) [protected]

Drop a test table.

Parameters:
$tablenamethe name of the test table.

Definition at line 882 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

drop_test_tables ( tablenames) [protected]

Convenience method for calling drop_test_table repeatedly.

Parameters:
array$tablenamesan array of table names.

Definition at line 897 of file simpletestlib.php.

Here is the call graph for this function:

load_test_data ( table,
array cols,
array data 
) [protected]

Load a table with some rows of data. A typical call would look like:

$config = $this->load_test_data('config_plugins', array('plugin', 'name', 'value'), array( array('frog', 'numlegs', 2), array('frog', 'sound', 'croak'), array('frog', 'action', 'jump'), ));

Parameters:
string$tablethe table name.
array$colsthe columns to fill.
array$datathe data to load.
Returns:
array $objects corresponding to $data.

Definition at line 918 of file simpletestlib.php.

Here is the caller graph for this function:

revert_global_user_id ( ) [protected]

Revert $USER->id to the real value.

Definition at line 738 of file simpletestlib.php.

Here is the call graph for this function:

revert_to_real_cfg ( ) [protected]

Revert to using the real $CFG for all future queries.

Definition at line 711 of file simpletestlib.php.

Here is the call graph for this function:

revert_to_real_db ( ) [protected]

Revert to using the test database for all future queries.

Definition at line 689 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

switch_global_user_id ( userid) [protected]

Switch $USER->id to a test value.

It might be worth making this method do more robuse $USER switching in future, however, this is sufficient for my needs at present.

Definition at line 725 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

switch_to_test_cfg ( ) [protected]

Switch to using the test $CFG for all queries until further notice.

Definition at line 700 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:

switch_to_test_db ( ) [protected]

Switch to using the test database for all queries until further notice.

Definition at line 678 of file simpletestlib.php.

Here is the call graph for this function:

Here is the caller graph for this function:


Field Documentation

$testcfg [protected]

Definition at line 643 of file simpletestlib.php.

$testdb [protected]

Definition at line 638 of file simpletestlib.php.


The documentation for this class was generated from the following file:
 All Data Structures Namespaces Files Functions Variables Enumerations