Skip to content

Commit 12f7678

Browse files
author
David Monllao
committed
Merge branch 'MDL-62867-missing_colon_in_query' of https://github.com/bitumin/moodle
2 parents fdab90a + d2029ef commit 12f7678

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

tag/classes/tag.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ public static function add_item_tag($component, $itemtype, $itemid, context $con
907907
}
908908

909909
$ordering = $DB->get_field_sql('SELECT MAX(ordering) FROM {tag_instance} ti
910-
WHERE ti.itemtype = :itemtype AND ti.itemid = itemid AND
910+
WHERE ti.itemtype = :itemtype AND ti.itemid = :itemid AND
911911
ti.component = :component' . $usersql, $params);
912912

913913
return $tag->add_instance($component, $itemtype, $itemid, $context, $ordering + 1, $tiuserid);

tag/tests/taglib_test.php

+47
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,53 @@ public function test_add_remove_item_tag() {
9494
$this->assertEquals(0, $DB->count_records('tag_instance'));
9595
}
9696

97+
/**
98+
* Test add_item_tag function correctly calculates the ordering for a new tag.
99+
*/
100+
public function test_add_tag_ordering_calculation() {
101+
global $DB;
102+
103+
$user1 = $this->getDataGenerator()->create_user();
104+
$course1 = $this->getDataGenerator()->create_course();
105+
$course2 = $this->getDataGenerator()->create_course();
106+
$book1 = $this->getDataGenerator()->create_module('book', array('course' => $course1->id));
107+
$now = time();
108+
$chapter1id = $DB->insert_record('book_chapters', (object) [
109+
'bookid' => $book1->id,
110+
'hidden' => 0,
111+
'timecreated' => $now,
112+
'timemodified' => $now,
113+
'importsrc' => '',
114+
'content' => '',
115+
'contentformat' => FORMAT_HTML,
116+
]);
117+
118+
// Create a tag (ordering should start at 1).
119+
$ti1 = core_tag_tag::add_item_tag('core', 'course', $course1->id,
120+
context_course::instance($course1->id), 'A random tag for course 1');
121+
$this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti1]));
122+
123+
// Create another tag with a common component, itemtype and itemid (should increase the ordering by 1).
124+
$ti2 = core_tag_tag::add_item_tag('core', 'course', $course1->id,
125+
context_course::instance($course1->id), 'Another random tag for course 1');
126+
$this->assertEquals(2, $DB->get_field('tag_instance', 'ordering', ['id' => $ti2]));
127+
128+
// Create a new tag with the same component and itemtype, but different itemid (should start counting from 1 again).
129+
$ti3 = core_tag_tag::add_item_tag('core', 'course', $course2->id,
130+
context_course::instance($course2->id), 'A random tag for course 2');
131+
$this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti3]));
132+
133+
// Create a new tag with a different itemtype (should start counting from 1 again).
134+
$ti4 = core_tag_tag::add_item_tag('core', 'user', $user1->id,
135+
context_user::instance($user1->id), 'A random tag for user 1');
136+
$this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti4]));
137+
138+
// Create a new tag with a different component (should start counting from 1 again).
139+
$ti5 = core_tag_tag::add_item_tag('mod_book', 'book_chapters', $chapter1id,
140+
context_module::instance($book1->cmid), 'A random tag for a book chapter');
141+
$this->assertEquals(1, $DB->get_field('tag_instance', 'ordering', ['id' => $ti5]));
142+
}
143+
97144
/**
98145
* Test that tag_assign function throws an exception.
99146
* This function was deprecated in 3.1

0 commit comments

Comments
 (0)