From 27dda89ffece14e437869fa19f9ec10678de7dcb Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Thu, 11 Sep 2008 13:47:40 +0000 Subject: [PATCH] MDL-15666 - changes to the portfolio caller tests now that i changed the api again (adding load_data), rather than add a call to it to all the callers, i abstracted the caller setup into the parent class (portfoliolib_test) --- lib/simpletest/testportfoliolib.php | 32 +++++++++++++++++++ .../test_assignment_portfolio_callers.php | 7 ++-- .../test_chat_portfolio_callers.php | 7 +--- .../test_data_portfolio_callers.php | 10 ++---- .../test_forum_portfolio_callers.php | 5 +-- .../test_glossary_portfolio_callers.php | 7 ++-- 6 files changed, 41 insertions(+), 27 deletions(-) diff --git a/lib/simpletest/testportfoliolib.php b/lib/simpletest/testportfoliolib.php index 780df57897..cd72e849bb 100755 --- a/lib/simpletest/testportfoliolib.php +++ b/lib/simpletest/testportfoliolib.php @@ -95,6 +95,14 @@ class portfolio_caller_test extends portfolio_caller_base { public static function display_name() { return "Test caller subclass"; } + + public function load_data() { + + } + + public static function expected_callbackargs() { + return array(); + } } /** @@ -162,6 +170,30 @@ class portfoliolib_test extends UnitTestCase { } $this->assertTrue($gotexception); } + + /** + * does everything we need to set up a new caller + * so each subclass doesn't have to implement this + * + * @param string $class name of caller class to generate (this class def must be already loaded) + * @param array $callbackargs the arguments to pass the constructor of the caller + * @param int $userid a userid the subclass has generated + * + * @return portfolio_caller_base subclass + */ + protected function setup_caller($class, $callbackargs, $user=null) { + global $DB; + $caller = new $class($callbackargs); + $caller->set('exporter', new mock_exporter()); + if (is_numeric($user)) { + $user = $DB->get_record('user', array('id' => $user)); + } + if (is_object($user)) { + $caller->set('user', $user); + } + $caller->load_data(); + return $caller; + } } // Load tests for various modules diff --git a/mod/assignment/simpletest/test_assignment_portfolio_callers.php b/mod/assignment/simpletest/test_assignment_portfolio_callers.php index f02e414346..9269d4b309 100644 --- a/mod/assignment/simpletest/test_assignment_portfolio_callers.php +++ b/mod/assignment/simpletest/test_assignment_portfolio_callers.php @@ -30,11 +30,8 @@ class testAssignmentPortfolioCallers extends portfoliolib_test { $submissions = $DB->get_records('assignment_submissions', array('assignment' => $first_module->id)); $first_submission = reset($submissions); - $callbackargs = array('assignmentid' => $cm->id, 'userid' => $USER->id); - $this->caller = new assignment_portfolio_caller($callbackargs); - $this->caller->set('exporter', new mock_exporter()); - $user = $DB->get_record('user', array('id' => $first_submission->userid)); - $this->caller->set('user', $user); + $callbackargs = array('id' => $cm->id); + $this->caller = parent::setup_caller('assignment_portfolio_caller', array('id' => $cm->id), $first_submission->userid); } public function tearDown() { diff --git a/mod/chat/simpletest/test_chat_portfolio_callers.php b/mod/chat/simpletest/test_chat_portfolio_callers.php index 37158b55cc..56d2ce2487 100644 --- a/mod/chat/simpletest/test_chat_portfolio_callers.php +++ b/mod/chat/simpletest/test_chat_portfolio_callers.php @@ -29,12 +29,7 @@ class testChatPortfolioCallers extends portfoliolib_test { $cm = get_coursemodule_from_instance($this->module_type, $first_module->id); $userid = $DB->get_field('chat_users', 'userid', array('chatid' => $first_module->id)); - $callbackargs = array('id' => $cm->id); - $this->caller = new chat_portfolio_caller($callbackargs); - $this->caller->set('exporter', new mock_exporter()); - - $user = $DB->get_record('user', array('id' => $userid)); - $this->caller->set('user', $user); + $this->caller = parent::setup_caller('chat_portfolio_caller', array('id' => $cm->id), $userid); } public function tearDown() { diff --git a/mod/data/simpletest/test_data_portfolio_callers.php b/mod/data/simpletest/test_data_portfolio_callers.php index 0080b57935..42af481c2e 100644 --- a/mod/data/simpletest/test_data_portfolio_callers.php +++ b/mod/data/simpletest/test_data_portfolio_callers.php @@ -46,14 +46,8 @@ class testDataPortfolioCallers extends portfoliolib_test { } // Callback args required: id, record, delimiter_name, exporttype - - $callbackargs = array('id' => $cm->id, 'record' => $recordid); - $this->caller_single = new data_portfolio_caller($callbackargs); - $this->caller_single->set('exporter', new mock_exporter()); - - unset($callbackargs['record']); - $this->caller = new data_portfolio_caller($callbackargs); - $this->caller->set('exporter', new mock_exporter()); + $this->caller_single = parent::setup_caller('data_portfolio_caller', array('id' => $cm->id, 'record' => $recordid)); + $this->caller = parent::setup_caller('data_portfolio_caller', array('id' => $cm->id)); } public function tearDown() { diff --git a/mod/forum/simpletest/test_forum_portfolio_callers.php b/mod/forum/simpletest/test_forum_portfolio_callers.php index eeb1838c13..00271b1ed4 100644 --- a/mod/forum/simpletest/test_forum_portfolio_callers.php +++ b/mod/forum/simpletest/test_forum_portfolio_callers.php @@ -44,10 +44,7 @@ class testForumPortfolioCallers extends portfoliolib_test { $first_post = reset($posts); $callbackargs = array('postid' => $first_post->id, 'discussionid' => $first_discussion->id); - $this->caller = new forum_portfolio_caller($callbackargs); - $this->caller->set('exporter', new mock_exporter()); - $user = $DB->get_record('user', array('id' => $first_post->userid)); - $this->caller->set('user', $user); + $this->caller = parent::setup_caller('forum_portfolio_caller', $callbackargs, $first_post->userid); } public function tearDown() { diff --git a/mod/glossary/simpletest/test_glossary_portfolio_callers.php b/mod/glossary/simpletest/test_glossary_portfolio_callers.php index d4f1c394c8..658b0f5ddf 100644 --- a/mod/glossary/simpletest/test_glossary_portfolio_callers.php +++ b/mod/glossary/simpletest/test_glossary_portfolio_callers.php @@ -33,10 +33,9 @@ class testGlossaryPortfolioCallers extends portfoliolib_test { $first_entry = reset($this->entries); $callbackargs = array('id' => $cm->id, 'entryid' => $first_entry->id); - $this->entry_caller = new glossary_entry_portfolio_caller($callbackargs); - $this->entry_caller->set('exporter', new mock_exporter()); - $this->csv_caller = new glossary_csv_portfolio_caller($callbackargs); - $this->csv_caller->set('exporter', new mock_exporter()); + $this->entry_caller = parent::setup_caller('glossary_entry_portfolio_caller', $callbackargs); + + $this->csv_caller = parent::setup_caller('glossary_csv_portfolio_caller', $callbackargs); } public function tearDown() { -- 2.39.5