@@ -94,6 +94,53 @@ public function test_add_remove_item_tag() {
94
94
$ this ->assertEquals (0 , $ DB ->count_records ('tag_instance ' ));
95
95
}
96
96
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
+
97
144
/**
98
145
* Test that tag_assign function throws an exception.
99
146
* This function was deprecated in 3.1
0 commit comments