]> git.mjollnir.org Git - moodle.git/commitdiff
Improved the workshop constructor to support unit testing
authorDavid Mudrak <david.mudrak@gmail.com>
Mon, 4 Jan 2010 18:12:32 +0000 (18:12 +0000)
committerDavid Mudrak <david.mudrak@gmail.com>
Mon, 4 Jan 2010 18:12:32 +0000 (18:12 +0000)
Thanks to Tim Hunt for the hint.

mod/workshop/allocation/random/simpletest/testallocator.php
mod/workshop/form/accumulative/simpletest/teststrategy.php
mod/workshop/form/numerrors/simpletest/teststrategy.php
mod/workshop/locallib.php
mod/workshop/simpletest/testlocallib.php [moved from mod/workshop/simpletest/testworkshopapi.php with 100% similarity]

index c72d05ebeccb04293a6410a8b9c27a811bce1e4a..df101e0f6bbaea6da321238427834f66a31d77fd 100644 (file)
@@ -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);
     }
 
index 4dcf21ea2d2229dbb078c15edbdb8241eaa4d4c8..34e6b219b6bc172757c5293ee516a6abae924e2c 100644 (file)
@@ -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);
     }
 
index 7f03926dc125b895169ccb180fff64ecdd08554e..72508fcb3939e1d7aa21ed7dbacc73b12a804766 100644 (file)
@@ -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);
     }
 
index d5144679891b9b740ede5230ca859c8e33bbb5fc..122dbe606ffe11c2a991ccf599efb3fc85520267 100644 (file)
@@ -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
     }
 
     ////////////////////////////////////////////////////////////////////////////////