Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/simpletest/portfolio_testclass.php
Go to the documentation of this file.
00001 <?php
00002 
00004 //                                                                       //
00005 // NOTICE OF COPYRIGHT                                                   //
00006 //                                                                       //
00007 // Moodle - Modular Object-Oriented Dynamic Learning Environment         //
00008 //          http://moodle.org                                            //
00009 //                                                                       //
00010 // Copyright (C) 1999 onwards Martin Dougiamas  http://dougiamas.com     //
00011 //                                                                       //
00012 // This program is free software; you can redistribute it and/or modify  //
00013 // it under the terms of the GNU General Public License as published by  //
00014 // the Free Software Foundation; either version 2 of the License, or     //
00015 // (at your option) any later version.                                   //
00016 //                                                                       //
00017 // This program is distributed in the hope that it will be useful,       //
00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of        //
00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         //
00020 // GNU General Public License for more details:                          //
00021 //                                                                       //
00022 //          http://www.gnu.org/copyleft/gpl.html                         //
00023 //                                                                       //
00025 
00034 if (!defined('MOODLE_INTERNAL')) {
00035     die('Direct access to this script is forbidden.');    
00036 }
00037 
00038 require_once("$CFG->libdir/portfoliolib.php");
00039 require_once("$CFG->libdir/portfolio/exporter.php");
00040 require_once("$CFG->libdir/portfolio/plugin.php");
00041 
00042 class portfolio_plugin_test extends portfolio_plugin_push_base {
00043     public function expected_time($callertime){
00044         return $callertime;
00045     }
00046 
00047     public function prepare_package() {
00048         return true;
00049     }
00050 
00051     public function send_package() {
00052         return true;
00053     }
00054 
00055     public function get_interactive_continue_url() {
00056         return '';
00057     }
00058 
00059     public static function get_name() {
00060         return '';
00061     }
00062 }
00063 
00064 class portfolio_caller_test extends portfolio_caller_base {
00065     private $content;
00066 
00067     public function __construct($content) {
00068         $this->content = $content;
00069     }
00070 
00071     public function expected_time() {
00072         return PORTFOLIO_TIME_LOW;
00073     }
00074 
00075     public function get_navigation() {
00076         $extranav = array('name' => 'Test caller class', 'link' => $this->get_return_url());
00077         return array($extranav, 'test');
00078     }
00079 
00080     public function get_sha1(){
00081         return sha1($this->content);
00082     }
00083 
00084     public function prepare_package() {
00085 
00086     }
00087 
00088     public function get_return_url() {
00089         return '';
00090     }
00091 
00092     public function check_permissions() {
00093         return true;
00094     }
00095 
00096     public static function display_name() {
00097         return "Test caller subclass";
00098     }
00099 
00100     public function load_data() {
00101 
00102     }
00103 
00104     public static function expected_callbackargs() {
00105         return array();
00106     }
00107     public static function base_supported_formats() {
00108         return array(PORTFOLIO_FORMAT_RICH, PORTFOLIO_FORMAT_FILE);
00109     }
00110     public function set_context($PAGE) {
00111         $PAGE->set_context(get_system_context());
00112     }
00113 }
00114 
00115 class portfolio_exporter_test extends portfolio_exporter {
00116     private $files;
00117 
00118     public function write_new_file($content, $name) {
00119         if (empty($this->files)) {
00120             $this->files = array();
00121         }
00122         $this->files[] = new portfolio_fake_file($content, $name);
00123     }
00124 
00125     public function copy_existing_file($oldfile) {
00126         throw new portfolio_exception('notimplemented', 'portfolio', 'files api test');
00127     }
00128 
00129     public function get_tempfiles() {
00130         return $this->files;
00131     }
00132 
00133     public function zip_tempfiles() {
00134         return new portfolio_fake_file('fake content zipfile', 'portfolio-export.zip');
00135     }
00136 }
00137 
00141 class portfolio_fake_file {
00142 
00143     private $name;
00144     private $content;
00145     private $hash;
00146     private $size;
00147 
00148     public function __construct($content, $name) {
00149         $this->content = $content;
00150         $this->name = $name;
00151         $this->hash = sha1($content);
00152         $this->size = strlen($content);
00153     }
00154 
00155     public function get_contenthash() {
00156         return $this->hash;
00157     }
00158 
00159     public function get_filename() {
00160         return $this->name;
00161     }
00162 
00163     public function get_filesize() {
00164         return $this->size;
00165     }
00166 }
00167 
00172 Mock::generate('portfolio_caller_test', 'mock_caller');
00173 Mock::generate('portfolio_plugin_test', 'mock_plugin');
00174 
00179 Mock::generatePartial('portfolio_plugin_test', 'partialmock_plugin', array('send_package'));
00180 Mock::generatePartial('portfolio_exporter_test', 'partialmock_exporter', array('process_stage_confirm',
00181                                                                                'process_stage_cleanup',
00182                                                                                'log_transfer',
00183                                                                                'save',
00184                                                                                'rewaken_object'));
00185 
00186 
00187 // Generate a mock class for each plugin subclass present
00188 $portfolio_plugins = get_list_of_plugins('portfolio');
00189 foreach ($portfolio_plugins as $plugin) {
00190     require_once($CFG->dirroot . "/portfolio/$plugin/lib.php");
00191     Mock::generatePartial("portfolio_plugin_$plugin", "partialmock_plugin_$plugin", array('send_package'));
00192 }
00193 
00194 class portfoliolib_test extends UnitTestCaseUsingDatabase {
00195     private $olduser;
00196 
00197     protected $testtables = array(
00198                 'lib' => array(
00199                     'portfolio_instance', 'portfolio_instance_user', 'portfolio_instance_config', 
00200                         'user', 'course', 'course_categories'));
00201 
00202     function setup() {
00203         global $USER;
00204         parent::setup();
00205         
00206         $this->switch_to_test_db(); // Switch to test DB for all the execution
00207 
00208         foreach ($this->testtables as $dir => $tables) {
00209             $this->create_test_tables($tables, $dir); // Create tables
00210         }
00211         
00212         // It is necessary to store $USER object because some subclasses use generator
00213         // stuff which breaks $USER
00214         $this->olduser = $USER;
00215     }
00216 
00217     function tearDown() {
00218         global $USER;
00219         $USER = $this->olduser;
00220         parent::tearDown();
00221     }
00222 
00223     function test_construct_dupe_instance() {
00224         $gotexception = false;
00225         try {
00226             $plugin1 = portfolio_plugin_base::create_instance('download', 'download1', array());
00227             $plugin2 = portfolio_plugin_base::create_instance('download', 'download2', array());
00228             $test1 = new portfolio_plugin_download($plugin1->get('id'));
00229         } catch (portfolio_exception $e) {
00230             $this->assertEqual('multipleinstancesdisallowed', $e->errorcode);
00231             $gotexception = true;
00232         }
00233         $this->assertTrue($gotexception);
00234     }
00235 
00246     protected function setup_caller($class, $callbackargs, $user=null) {
00247         global $DB;
00248         $caller = new $class($callbackargs);
00249         $caller->set('exporter', new partialmock_exporter($this));
00250         if (is_numeric($user)) {
00251             $user = $DB->get_record('user', array('id' => $user));
00252         }
00253         if (is_object($user)) {
00254             $caller->set('user', $user);
00255         }
00256         $caller->load_data();
00257         // set any format
00258         $caller->get('exporter')->set('format', array_shift($caller->supported_formats()));
00259         return $caller;
00260     }
00261 
00262     public function test_caller_with_plugins() {
00263         if (!empty($this->caller)) {
00264             $plugins = get_list_of_plugins('portfolio');
00265 
00266             foreach ($plugins as $plugin) {
00267                 // Instantiate a fake plugin instance
00268                 $plugin_class = "partialmock_plugin_$plugin";
00269                 $plugin = new $plugin_class($this);
00270 
00271                 // figure out our format intersection and test all of them.
00272                 $formats = portfolio_supported_formats_intersect($this->caller->supported_formats(), $plugin->supported_formats());
00273                 if (count($formats) == 0) {
00274                     // bail. no common formats.
00275                     continue;
00276                 }
00277 
00278                 foreach ($formats as $format) {
00279                     // Create a new fake exporter
00280                     $exporter =& $this->caller->get('exporter'); // new partialmock_exporter(&$this);
00281                     $exporter->set('caller', $this->caller);
00282                     $exporter->set('instance', $plugin);
00283                     $exporter->set('format', $format);
00284                     $this->caller->set_export_config(array('format' => $format));
00285                     $plugin->set_export_config(array('format' => $format));
00286                     $exporter->set('user', $this->caller->get('user'));
00287 
00288                     $exception = false;
00289                     try {
00290                         $exporter->process_stage_package();
00291                     } catch (Exception $e) {
00292                         $exception = $e->getMessage();
00293                     }
00294 
00295                     $this->assertFalse($exception, "Unwanted exception: $exception");
00296                 }
00297             }
00298         }
00299     }
00300 }
00301 
 All Data Structures Namespaces Files Functions Variables Enumerations