From f2f66ea4afae69cb90354fa621ee5998a59c0efc Mon Sep 17 00:00:00 2001 From: David Monllao Date: Tue, 18 Dec 2012 11:21:04 +0800 Subject: [PATCH] MDL-37046 behat: Some unit tests --- admin/tool/behat/tests/tool_behat_test.php | 184 +++++++++++++++++++++ 1 file changed, 184 insertions(+) create mode 100644 admin/tool/behat/tests/tool_behat_test.php diff --git a/admin/tool/behat/tests/tool_behat_test.php b/admin/tool/behat/tests/tool_behat_test.php new file mode 100644 index 00000000000..5dc0530672b --- /dev/null +++ b/admin/tool/behat/tests/tool_behat_test.php @@ -0,0 +1,184 @@ +. + +/** + * Unit tests for admin/tool/behat + * + * @package tool_behat + * @copyright 2012 David Monllaó + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot.'/' . $CFG->admin .'/tool/behat/locallib.php'); + +/** + * Allows access to internal methods without exposing them + */ +class testable_tool_behat extends tool_behat { + + /** + * @see parent::merge_config() + * @param mixed $config + * @param mixed $localconfig + * @return mixed + */ + public static function merge_config($config, $localconfig) { + return parent::merge_config($config, $localconfig); + } + + /** + * @see parent::get_config_file_contents() + * @param string $prefix + * @param array $features + * @param array $stepsdefinitions + * @return string + */ + public static function get_config_file_contents($prefix, $features, $stepsdefinitions) { + return parent::get_config_file_contents($prefix, $features, $stepsdefinitions); + } +} + +/** + * Tool behat tests + */ +class tool_behat_testcase extends advanced_testcase { + + + public function test_switch_environment() { + + // Only run the tests if behat dependencies are installed. + // We don't need to pre-check PHPUnit initialisation because we are running on it. + if (version_compare(PHP_VERSION, '5.4.0', '>=') && tool_behat::are_behat_dependencies_installed()) { + tool_behat::switchenvironment('enable'); + $this->assertTrue(tool_behat::is_test_mode_enabled()); + $this->assertFalse(tool_behat::is_test_environment_running()); + + // We trigger a debugging() if it's already enabled. + tool_behat::switchenvironment('enable'); + $this->assertDebuggingCalled(); + + tool_behat::switchenvironment('disable'); + $this->assertFalse(tool_behat::is_test_mode_enabled()); + $this->assertFalse(tool_behat::is_test_environment_running()); + + // We trigger a debugging() if it's already enabled. + tool_behat::switchenvironment('disable'); + $this->assertDebuggingCalled(); + + // Ensure all continues disabled. + $this->assertFalse(tool_behat::is_test_mode_enabled()); + $this->assertFalse(tool_behat::is_test_environment_running()); + } + } + + public function test_merge_configs() { + + // Simple default config. + $array1 = array( + 'the' => 'same', + 'simple' => 'value', + 'array' => array( + 'one' => 'arrayvalue1', + 'two' => 'arrayvalue2' + ) + ); + + // Simple override. + $array2 = array( + 'simple' => 'OVERRIDDEN1', + 'array' => array( + 'one' => 'OVERRIDDEN2' + ), + 'newprofile' => array( + 'anotherlevel' => array( + 'andanotherone' => array( + 'list1', + 'list2' + ) + ) + ) + ); + + $array = testable_tool_behat::merge_config($array1, $array2); + + // Overriddes are applied. + $this->assertEquals('OVERRIDDEN1', $array['simple']); + $this->assertEquals('OVERRIDDEN2', $array['array']['one']); + + // Other values are respected. + $this->assertNotEmpty($array['array']['two']); + + // Completely new nodes are added. + $this->assertNotEmpty($array['newprofile']); + $this->assertNotEmpty($array['newprofile']['anotherlevel']['andanotherone']); + $this->assertEquals('list1', $array['newprofile']['anotherlevel']['andanotherone'][0]); + $this->assertEquals('list2', $array['newprofile']['anotherlevel']['andanotherone'][1]); + + // Complex override changing vectors to scalars and scalars to vectors. + $array2 = array( + 'simple' => array( + 'simple' => 'should', + 'be' => 'overridden', + 'by' => 'this-array' + ), + 'array' => 'one' + ); + + $array = testable_tool_behat::merge_config($array1, $array2); + + // Overrides applied. + $this->assertNotEmpty($array['simple']); + $this->assertNotEmpty($array['array']); + $this->assertTrue(is_array($array['simple'])); + $this->assertFalse(is_array($array['array'])); + + // Other values are maintained. + $this->assertEquals('same', $array['the']); + } + + public function test_config_file_contents() { + global $CFG; + + unset($CFG->behatconfig); + + // List. + $features = array( + 'feature1', + 'feature2', + 'feature3' + ); + + // Associative array. + $stepsdefinitions = array( + 'micarro' => '/me/lo/robaron', + 'anoche' => '/cuando/yo/dormia' + ); + + $contents = testable_tool_behat::get_config_file_contents('/i/am/a/prefix/', $features, $stepsdefinitions); + + $this->assertContains('features: /i/am/a/prefix/lib/behat/features', $contents); + $this->assertContains('micarro: /me/lo/robaron', $contents); + $this->assertContains('base_url: \'' . $CFG->test_wwwroot . '\'', $contents); + $this->assertContains('class: behat_init_context', $contents); + $this->assertContains('- feature1', $contents); + $this->assertContains('- feature3', $contents); + + } +} +