Skip to content

Commit

Permalink
Merge branch 'MDL-43835' of https://github.com/mr-russ/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
Damyon Wiese committed Jun 23, 2014
2 parents c436a90 + d2c58b9 commit a51201a
Show file tree
Hide file tree
Showing 16 changed files with 141 additions and 97 deletions.
4 changes: 2 additions & 2 deletions backup/util/dbops/tests/backup_dbops_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function setUp() {
$page = $this->getDataGenerator()->create_module('page', array('course'=>$course->id), array('section'=>3));
$coursemodule = $DB->get_record('course_modules', array('id'=>$page->cmid));

$this->moduleid = $coursemodule->id;
$this->moduleid = $page->cmid;
$this->sectionid = $DB->get_field("course_sections", 'id', array("section"=>$coursemodule->section, "course"=>$course->id));
$this->courseid = $coursemodule->course;
$this->userid = 2; // admin
Expand Down Expand Up @@ -180,7 +180,7 @@ function test_backup_controller_dbops_includes_files() {
$this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);

// A MODE_SAMESITE controller - should not include files
$bc = new mock_backup_controller4dbops(backup::TYPE_1COURSE, $this->moduleid, backup::FORMAT_MOODLE,
$bc = new mock_backup_controller4dbops(backup::TYPE_1COURSE, $this->courseid, backup::FORMAT_MOODLE,
backup::INTERACTIVE_NO, backup::MODE_SAMESITE, $this->userid);
$this->assertEquals(backup_controller_dbops::backup_includes_files($bc->get_backupid()), 0);
}
Expand Down
108 changes: 61 additions & 47 deletions backup/util/structure/tests/structure_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,22 @@
*/
class backup_structure_testcase extends advanced_testcase {

protected $forumid; // To store the inserted forum->id
protected $contextid; // Official contextid for these tests
/** @var int Store the inserted forum->id for use in test functions */
protected $forumid;
/** @var int Store the inserted discussion1->id for use in test functions */
protected $discussionid1;
/** @var int Store the inserted discussion2->id for use in test functions */
protected $discussionid2;
/** @var int Store the inserted post1->id for use in test functions */
protected $postid1;
/** @var int Store the inserted post2->id for use in test functions */
protected $postid2;
/** @var int Store the inserted post3->id for use in test functions */
protected $postid3;
/** @var int Store the inserted post4->id for use in test functions */
protected $postid4;
/** @var int Official contextid for these tests */
protected $contextid;


protected function setUp() {
Expand Down Expand Up @@ -72,47 +86,47 @@ private function fill_records() {

// Create two discussions
$discussion1 = (object)array('course' => 1, 'forum' => $this->forumid, 'name' => 'd1', 'userid' => 100, 'groupid' => 200);
$d1id = $DB->insert_record('forum_discussions', $discussion1);
$this->discussionid1 = $DB->insert_record('forum_discussions', $discussion1);
$discussion2 = (object)array('course' => 1, 'forum' => $this->forumid, 'name' => 'd2', 'userid' => 101, 'groupid' => 201);
$d2id = $DB->insert_record('forum_discussions', $discussion2);
$this->discussionid2 = $DB->insert_record('forum_discussions', $discussion2);

// Create four posts
$post1 = (object)array('discussion' => $d1id, 'userid' => 100, 'subject' => 'p1', 'message' => 'm1');
$p1id = $DB->insert_record('forum_posts', $post1);
$post2 = (object)array('discussion' => $d1id, 'parent' => $p1id, 'userid' => 102, 'subject' => 'p2', 'message' => 'm2');
$p2id = $DB->insert_record('forum_posts', $post2);
$post3 = (object)array('discussion' => $d1id, 'parent' => $p2id, 'userid' => 103, 'subject' => 'p3', 'message' => 'm3');
$p3id = $DB->insert_record('forum_posts', $post3);
$post4 = (object)array('discussion' => $d2id, 'userid' => 101, 'subject' => 'p4', 'message' => 'm4');
$p4id = $DB->insert_record('forum_posts', $post4);
$post1 = (object)array('discussion' => $this->discussionid1, 'userid' => 100, 'subject' => 'p1', 'message' => 'm1');
$this->postid1 = $DB->insert_record('forum_posts', $post1);
$post2 = (object)array('discussion' => $this->discussionid1, 'parent' => $this->postid1, 'userid' => 102, 'subject' => 'p2', 'message' => 'm2');
$this->postid2 = $DB->insert_record('forum_posts', $post2);
$post3 = (object)array('discussion' => $this->discussionid1, 'parent' => $this->postid2, 'userid' => 103, 'subject' => 'p3', 'message' => 'm3');
$this->postid3 = $DB->insert_record('forum_posts', $post3);
$post4 = (object)array('discussion' => $this->discussionid2, 'userid' => 101, 'subject' => 'p4', 'message' => 'm4');
$this->postid4 = $DB->insert_record('forum_posts', $post4);
// With two related file
$f1_post1 = (object)array(
'contenthash' => 'testp1', 'contextid' => $this->contextid, 'component'=>'mod_forum',
'filearea' => 'post', 'filename' => 'tp1', 'itemid' => $p1id,
'filearea' => 'post', 'filename' => 'tp1', 'itemid' => $this->postid1,
'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
'pathnamehash' => 'testp1'
);
$DB->insert_record('files', $f1_post1);
$f1_post2 = (object)array(
'contenthash' => 'testp2', 'contextid' => $this->contextid, 'component'=>'mod_forum',
'filearea' => 'attachment', 'filename' => 'tp2', 'itemid' => $p2id,
'filearea' => 'attachment', 'filename' => 'tp2', 'itemid' => $this->postid2,
'filesize' => 123, 'timecreated' => 0, 'timemodified' => 0,
'pathnamehash' => 'testp2'
);
$DB->insert_record('files', $f1_post2);

// Create two ratings
$rating1 = (object)array(
'contextid' => $this->contextid, 'userid' => 104, 'itemid' => $p1id, 'rating' => 2,
'contextid' => $this->contextid, 'userid' => 104, 'itemid' => $this->postid1, 'rating' => 2,
'scaleid' => -1, 'timecreated' => time(), 'timemodified' => time());
$r1id = $DB->insert_record('rating', $rating1);
$rating2 = (object)array(
'contextid' => $this->contextid, 'userid' => 105, 'itemid' => $p1id, 'rating' => 3,
'contextid' => $this->contextid, 'userid' => 105, 'itemid' => $this->postid1, 'rating' => 3,
'scaleid' => -1, 'timecreated' => time(), 'timemodified' => time());
$r2id = $DB->insert_record('rating', $rating2);

// Create 1 reads
$read1 = (object)array('userid' => 102, 'forumid' => $this->forumid, 'discussionid' => $d2id, 'postid' => $p4id);
$read1 = (object)array('userid' => 102, 'forumid' => $this->forumid, 'discussionid' => $this->discussionid2, 'postid' => $this->postid4);
$DB->insert_record('forum_read', $read1);
}

Expand Down Expand Up @@ -195,11 +209,11 @@ function test_backup_structure_construct() {

// Let's add 1 optigroup with 4 elements
$alternative1 = new backup_optigroup_element('alternative1',
array('name', 'value'), '../../id', 1);
array('name', 'value'), '../../id', $this->postid1);
$alternative2 = new backup_optigroup_element('alternative2',
array('name', 'value'), backup::VAR_PARENTID, 2);
array('name', 'value'), backup::VAR_PARENTID, $this->postid2);
$alternative3 = new backup_optigroup_element('alternative3',
array('name', 'value'), '/forum/discussions/discussion/posts/post/id', 3);
array('name', 'value'), '/forum/discussions/discussion/posts/post/id', $this->postid3);
$alternative4 = new backup_optigroup_element('alternative4',
array('forumtype', 'forumname')); // Alternative without conditions
// Create the optigroup, adding one element
Expand Down Expand Up @@ -239,7 +253,7 @@ function test_backup_structure_construct() {
array(backup::VAR_PARENTID)
);

$read->set_source_table('forum_read', array('id' => '../../id'));
$read->set_source_table('forum_read', array('forumid' => '../../id'));

$inventeds->set_source_array(array((object)array('reason' => 'I love Moodle', 'version' => '1.0'),
(object)array('reason' => 'I love Moodle', 'version' => '2.0'))); // 2 object array
Expand Down Expand Up @@ -334,83 +348,83 @@ function test_backup_structure_construct() {
$ratarr[$node->nodeName] = $node->nodeValue;
}
}
$this->assertEquals($ratarr['userid'], $DB->get_field('rating', 'userid', array('id' => $ratarr['id'])));
$this->assertEquals($ratarr['itemid'], $DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])));
$this->assertEquals($ratarr['post_rating'], $DB->get_field('rating', 'rating', array('id' => $ratarr['id'])));
$this->assertEquals($DB->get_field('rating', 'userid', array('id' => $ratarr['id'])), $ratarr['userid']);
$this->assertEquals($DB->get_field('rating', 'itemid', array('id' => $ratarr['id'])), $ratarr['itemid']);
$this->assertEquals($DB->get_field('rating', 'rating', array('id' => $ratarr['id'])), $ratarr['post_rating']);
}

// Check forum has "blockeperiod" with value 0 (was declared by object instead of name)
$query = '/forum[blockperiod="0"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check forum is missing "completiondiscussions" (as we are using mock_skip_final_element)
$query = '/forum/completiondiscussions';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$this->assertEquals(0, $result->length);

// Check forum has "completionreplies" with value "original was 0, now changed" (because of mock_modify_final_element)
$query = '/forum[completionreplies="original was 0, now changed"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check forum has "completionposts" with value "intercepted!" (because of mock_final_element_interceptor)
$query = '/forum[completionposts="intercepted!"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check there isn't any alternative2 tag, as far as it hasn't source defined
$query = '//alternative2';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$this->assertEquals(0, $result->length);

// Check there are 4 "field1" elements
$query = '/forum/discussions/discussion/posts/post//field1';
$result = $xpath->query($query);
$this->assertEquals($result->length, 4);
$this->assertEquals(4, $result->length);

// Check first post has one name element with value "alternative1"
$query = '/forum/discussions/discussion/posts/post[@id="1"][name="alternative1"]';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid1.'"][name="alternative1"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check there are two "dupetest1" elements
$query = '/forum/discussions/discussion/posts/post//dupetest1';
$result = $xpath->query($query);
$this->assertEquals($result->length, 2);
$this->assertEquals(2, $result->length);

// Check second post has one name element with value "dupetest2"
$query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid2.'"]/dupetest2';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check element "dupetest2" of second post has one field1 element with value "2"
$query = '/forum/discussions/discussion/posts/post[@id="2"]/dupetest2[field1="2"]';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid2.'"]/dupetest2[field1="2"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check forth post has no name element
$query = '/forum/discussions/discussion/posts/post[@id="4"]/name';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid4.'"]/name';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$this->assertEquals(0, $result->length);

// Check 1st, 2nd and 3rd posts have no forumtype element
$query = '/forum/discussions/discussion/posts/post[@id="1"]/forumtype';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid1.'"]/forumtype';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$query = '/forum/discussions/discussion/posts/post[@id="2"]/forumtype';
$this->assertEquals(0, $result->length);
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid2.'"]/forumtype';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$query = '/forum/discussions/discussion/posts/post[@id="3"]/forumtype';
$this->assertEquals(0, $result->length);
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid3.'"]/forumtype';
$result = $xpath->query($query);
$this->assertEquals($result->length, 0);
$this->assertEquals(0, $result->length);

// Check 4th post has one forumtype element with value "general"
// (because it doesn't matches alternatives 1, 2, 3, then alternative 4,
// the one without conditions is being applied)
$query = '/forum/discussions/discussion/posts/post[@id="4"][forumtype="general"]';
$query = '/forum/discussions/discussion/posts/post[@id="'.$this->postid4.'"][forumtype="general"]';
$result = $xpath->query($query);
$this->assertEquals($result->length, 1);
$this->assertEquals(1, $result->length);

// Check annotations information against DB
// Count records in original tables
Expand Down
18 changes: 12 additions & 6 deletions lib/phpunit/tests/advanced_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,11 @@ public function test_database_reset() {

$this->assertEquals(0, $DB->count_records('user_preferences'));
$originaldisplayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
$this->assertEquals(1, $originaldisplayid);
$this->assertEquals(1, $DB->count_records('user_preferences'));

$numcourses = $DB->count_records('course');
$course = $this->getDataGenerator()->create_course();
$this->assertEquals(2, $course->id);
$this->assertEquals($numcourses + 1, $DB->count_records('course'));

$this->assertEquals(2, $DB->count_records('user'));
$DB->delete_records('user', array('id'=>1));
Expand All @@ -155,29 +156,34 @@ public function test_database_reset() {

$this->assertEquals(1, $DB->count_records('course')); // Only frontpage in new site.
$this->assertEquals(0, $DB->count_records('context_temp')); // Only frontpage in new site.

$numcourses = $DB->count_records('course');
$course = $this->getDataGenerator()->create_course();
$this->assertEquals(2, $course->id);
$this->assertEquals($numcourses + 1, $DB->count_records('course'));

$displayid = $DB->insert_record('user_preferences', array('userid'=>2, 'name'=> 'phpunittest', 'value'=>'x'));
$this->assertEquals($originaldisplayid, $displayid);

$this->assertEquals(2, $DB->count_records('user'));
$DB->delete_records('user', array('id'=>2));
$user = $this->getDataGenerator()->create_user();
$this->assertEquals(3, $user->id);
$this->assertEquals(2, $DB->count_records('user'));
$this->assertGreaterThan(2, $user->id);

$this->resetAllData();

$numcourses = $DB->count_records('course');
$course = $this->getDataGenerator()->create_course();
$this->assertEquals(2, $course->id);
$this->assertEquals($numcourses + 1, $DB->count_records('course'));

$this->assertEquals(2, $DB->count_records('user'));
$DB->delete_records('user', array('id'=>2));

$this->resetAllData();

$numcourses = $DB->count_records('course');
$course = $this->getDataGenerator()->create_course();
$this->assertEquals(2, $course->id);
$this->assertEquals($numcourses + 1, $DB->count_records('course'));

$this->assertEquals(2, $DB->count_records('user'));
}
Expand Down
Loading

0 comments on commit a51201a

Please sign in to comment.