Skip to content

Commit

Permalink
Merge branch 'MDL-35301-master' of git://github.com/FMCorz/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Sep 10, 2012
2 parents 5e51915 + 19ffa63 commit 1f4cd1f
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 0 deletions.
34 changes: 34 additions & 0 deletions course/tests/courselib_test.php
Expand Up @@ -25,9 +25,43 @@

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot.'/course/lib.php');

class courselib_testcase extends advanced_testcase {

public function test_create_course() {
global $DB;
$this->resetAfterTest(true);
$defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");

$course = new stdClass();
$course->fullname = 'Apu loves Unit Təsts';
$course->shortname = 'Spread the lŭve';
$course->idnumber = '123';
$course->summary = 'Awesome!';
$course->summaryformat = FORMAT_PLAIN;
$course->format = 'topics';
$course->newsitems = 0;
$course->numsections = 5;
$course->category = $defaultcategory;

$created = create_course($course);
$context = context_course::instance($created->id);

// Compare original and created.
$original = (array) $course;
$this->assertEquals($original, array_intersect_key((array) $created, $original));

// Ensure default section is created.
$sectioncreated = $DB->record_exists('course_sections', array('course' => $created->id, 'section' => 0));
$this->assertTrue($sectioncreated);

// Ensure blocks have been associated to the course.
$blockcount = $DB->count_records('block_instances', array('parentcontextid' => $context->id));
$this->assertGreaterThan(0, $blockcount);
}

public function test_reorder_sections() {
global $DB;
$this->resetAfterTest(true);
Expand Down
145 changes: 145 additions & 0 deletions course/tests/courserequest_test.php
@@ -0,0 +1,145 @@
<?php

// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Course request related unit tests
*
* @package core
* @category phpunit
* @copyright 2012 Frédéric Massart
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot.'/course/lib.php');

class courserequest_testcase extends advanced_testcase {

public function test_create_request() {
global $DB, $USER;
$this->resetAfterTest(true);

$defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
set_config('enablecourserequests', 1);
set_config('requestcategoryselection', 0);
set_config('defaultrequestcategory', $defaultcategory);

// Create some categories.
$cat1 = $this->getDataGenerator()->create_category();
$cat2 = $this->getDataGenerator()->create_category();
$cat3 = $this->getDataGenerator()->create_category();

// Basic course request.
$data = new stdClass();
$data->fullname = 'Həllo World!';
$data->shortname = 'Hi th€re!';
$data->summary_editor['text'] = 'Lorem Ipsum ©';
$data->summary_editor['format'] = FORMAT_HTML;
$data->reason = 'Because PHP Unit is cool.';
$cr = course_request::create($data);

$this->assertEquals($data->fullname, $cr->fullname);
$this->assertEquals($data->shortname, $cr->shortname);
$this->assertEquals($data->summary_editor['text'], $cr->summary);
$this->assertEquals($data->summary_editor['format'], $cr->summaryformat);
$this->assertEquals($data->reason, $cr->reason);
$this->assertEquals($USER->id, $cr->requester);
$this->assertEquals($defaultcategory, $cr->category);

// Request with category but category selection not allowed.
set_config('defaultrequestcategory', $cat2->id);
$data->category = $cat1->id;
$cr = course_request::create($data);
$this->assertEquals($cat2->id, $cr->category);

// Request with category different than default and category selection allowed.
set_config('defaultrequestcategory', $cat3->id);
set_config('requestcategoryselection', 1);
$data->category = $cat1->id;
$cr = course_request::create($data);
$this->assertEquals($cat1->id, $cr->category);
}

public function test_approve_request() {
global $DB;
$this->resetAfterTest(true);
$this->preventResetByRollback();

$this->setAdminUser();
$defaultcategory = $DB->get_field_select('course_categories', "MIN(id)", "parent=0");
set_config('enablecourserequests', 1);
set_config('requestcategoryselection', 0);
set_config('defaultrequestcategory', $defaultcategory);

// Create some categories.
$cat1 = $this->getDataGenerator()->create_category();
$cat2 = $this->getDataGenerator()->create_category();

$data = new stdClass();
$data->fullname = 'Həllo World!';
$data->shortname = 'Hi th€re!';
$data->summary_editor['text'] = 'Lorem Ipsum ©';
$data->summary_editor['format'] = FORMAT_HTML;
$data->reason = 'Because PHP Unit is cool.';

// Test without category.
$cr = course_request::create($data);
$id = $cr->approve();
$course = $DB->get_record('course', array('id' => $id));
$this->assertEquals($data->fullname, $course->fullname);
$this->assertEquals($data->shortname, $course->shortname);
$this->assertEquals($data->summary_editor['text'], $course->summary);
$this->assertEquals($data->summary_editor['format'], $course->summaryformat);
$this->assertEquals(1, $course->requested);
$this->assertEquals($defaultcategory, $course->category);

// Test with category.
set_config('requestcategoryselection', 1);
set_config('defaultrequestcategory', $cat2->id);
$data->shortname .= ' 2nd';
$data->category = $cat1->id;
$cr = course_request::create($data);
$id = $cr->approve();
$course = $DB->get_record('course', array('id' => $id));
$this->assertEquals($data->category, $course->category);
}

public function test_reject_request() {
global $DB;
$this->resetAfterTest(true);
$this->preventResetByRollback();
$this->setAdminUser();
set_config('enablecourserequests', 1);
set_config('requestcategoryselection', 0);
set_config('defaultrequestcategory', $DB->get_field_select('course_categories', "MIN(id)", "parent=0"));

$data = new stdClass();
$data->fullname = 'Həllo World!';
$data->shortname = 'Hi th€re!';
$data->summary_editor['text'] = 'Lorem Ipsum ©';
$data->summary_editor['format'] = FORMAT_HTML;
$data->reason = 'Because PHP Unit is cool.';

$cr = course_request::create($data);
$this->assertTrue($DB->record_exists('course_request', array('id' => $cr->id)));
$cr->reject('Sorry!');
$this->assertFalse($DB->record_exists('course_request', array('id' => $cr->id)));
}

}

0 comments on commit 1f4cd1f

Please sign in to comment.