From: David Mudrak Date: Mon, 4 Jan 2010 18:12:32 +0000 (+0000) Subject: Improved the workshop constructor to support unit testing X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=4efd7b5d5f7f7a24945699c0866a227cd4b90295;p=moodle.git Improved the workshop constructor to support unit testing Thanks to Tim Hunt for the hint. --- diff --git a/mod/workshop/allocation/random/simpletest/testallocator.php b/mod/workshop/allocation/random/simpletest/testallocator.php index c72d05ebec..df101e0f6b 100644 --- a/mod/workshop/allocation/random/simpletest/testallocator.php +++ b/mod/workshop/allocation/random/simpletest/testallocator.php @@ -68,10 +68,11 @@ class workshop_allocation_random_test extends UnitTestCase { protected $allocator; public function setUp() { - $cm = (object)array('id' => 3); - $course = (object)array('id' => 11); + $cm = new stdClass(); + $course = new stdClass(); + $context = new stdClass(); $workshop = (object)array('id' => 42); - $this->workshop = new workshop($workshop, $cm, $course); + $this->workshop = new workshop($workshop, $cm, $course, $context); $this->allocator = new testable_workshop_random_allocator($this->workshop); } diff --git a/mod/workshop/form/accumulative/simpletest/teststrategy.php b/mod/workshop/form/accumulative/simpletest/teststrategy.php index 4dcf21ea2d..34e6b219b6 100644 --- a/mod/workshop/form/accumulative/simpletest/teststrategy.php +++ b/mod/workshop/form/accumulative/simpletest/teststrategy.php @@ -67,10 +67,11 @@ class workshop_accumulative_strategy_test extends UnitTestCase { $this->realDB = $DB; $DB = new mockDB(); - $cm = (object)array('id' => 3); - $course = (object)array('id' => 11); + $cm = new stdClass(); + $course = new stdClass(); + $context = new stdClass(); $workshop = (object)array('id' => 42, 'strategy' => 'accumulative'); - $this->workshop = new workshop($workshop, $cm, $course); + $this->workshop = new workshop($workshop, $cm, $course, $context); $this->strategy = new testable_workshop_accumulative_strategy($this->workshop); } diff --git a/mod/workshop/form/numerrors/simpletest/teststrategy.php b/mod/workshop/form/numerrors/simpletest/teststrategy.php index 7f03926dc1..72508fcb39 100644 --- a/mod/workshop/form/numerrors/simpletest/teststrategy.php +++ b/mod/workshop/form/numerrors/simpletest/teststrategy.php @@ -70,10 +70,11 @@ class workshop_numerrors_strategy_test extends UnitTestCase { $this->realDB = $DB; $DB = new mockDB(); - $cm = (object)array('id' => 3); - $course = (object)array('id' => 11); + $cm = new stdClass(); + $course = new stdClass(); + $context = new stdClass(); $workshop = (object)array('id' => 42, 'strategy' => 'numerrors'); - $this->workshop = new workshop($workshop, $cm, $course); + $this->workshop = new workshop($workshop, $cm, $course, $context); $this->strategy = new testable_workshop_numerrors_strategy($this->workshop); } diff --git a/mod/workshop/locallib.php b/mod/workshop/locallib.php index d514467989..122dbe606f 100644 --- a/mod/workshop/locallib.php +++ b/mod/workshop/locallib.php @@ -80,18 +80,22 @@ class workshop { * @param stdClass $dbrecord Workshop instance data from {workshop} table * @param stdClass $cm Course module record as returned by {@link get_coursemodule_from_id()} * @param stdClass $course Course record from {course} table + * @param stdClass $context The context of the workshop instance */ - public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course) { + public function __construct(stdClass $dbrecord, stdClass $cm, stdClass $course, stdClass $context=null) { foreach ($dbrecord as $field => $value) { $this->{$field} = $value; } - $this->evaluation = 'best'; // todo make this configurable $this->cm = $cm; $this->course = $course; // beware - this replaces the standard course field in the instance table // this is intentional - IMO there should be no such field as it violates - // 3rd normal form with no real performance gain. This way I try to - // demonstrate how backwards compatibility could be achieved --mudrd8mz - $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id); + // 3rd normal form with no real performance gain + if (is_null($context)) { + $this->context = get_context_instance(CONTEXT_MODULE, $this->cm->id); + } else { + $this->context = $context; + } + $this->evaluation = 'best'; // todo make this configurable although we have no alternatives yet } //////////////////////////////////////////////////////////////////////////////// diff --git a/mod/workshop/simpletest/testworkshopapi.php b/mod/workshop/simpletest/testlocallib.php similarity index 100% rename from mod/workshop/simpletest/testworkshopapi.php rename to mod/workshop/simpletest/testlocallib.php