PageRenderTime 24ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/customfield/tests/api_test.php

https://github.com/mackensen/moodle
PHP | 254 lines | 153 code | 31 blank | 70 comment | 1 complexity | 1b086881cc86d070d3e829798eba4c17 MD5 | raw file
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. namespace core_customfield;
  17. /**
  18. * Functional test for class \core_customfield\api
  19. *
  20. * @package core_customfield
  21. * @category test
  22. * @copyright 2018 Toni Barbera <toni@moodle.com>
  23. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24. */
  25. class api_test extends \advanced_testcase {
  26. /**
  27. * Get generator.
  28. *
  29. * @return core_customfield_generator
  30. */
  31. protected function get_generator(): \core_customfield_generator {
  32. return $this->getDataGenerator()->get_plugin_generator('core_customfield');
  33. }
  34. /**
  35. * Help to assert that the given property in an array of object has the expected value
  36. *
  37. * @param array $expected
  38. * @param array $array array of objects with "get($property)" method
  39. * @param string $propertyname
  40. */
  41. protected function assert_property_in_array($expected, $array, $propertyname) {
  42. $this->assertEquals($expected, array_values(array_map(function($a) use ($propertyname) {
  43. return $a->get($propertyname);
  44. }, $array)));
  45. }
  46. /**
  47. * Tests for \core_customfield\api::move_category() behaviour.
  48. *
  49. * This replicates what is happening when categories are moved
  50. * in the interface using drag-drop.
  51. */
  52. public function test_move_category() {
  53. $this->resetAfterTest();
  54. // Create the categories.
  55. $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0];
  56. $id0 = $this->get_generator()->create_category($params)->get('id');
  57. $id1 = $this->get_generator()->create_category($params)->get('id');
  58. $id2 = $this->get_generator()->create_category($params)->get('id');
  59. $id3 = $this->get_generator()->create_category($params)->get('id');
  60. $id4 = $this->get_generator()->create_category($params)->get('id');
  61. $id5 = $this->get_generator()->create_category($params)->get('id');
  62. // Check order after re-fetch.
  63. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  64. $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
  65. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  66. // Move up 1 position.
  67. api::move_category(category_controller::create($id3), $id2);
  68. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  69. $this->assertEquals([$id0, $id1, $id3, $id2, $id4, $id5], array_keys($categories));
  70. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  71. // Move down 1 position.
  72. api::move_category(category_controller::create($id2), $id3);
  73. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  74. $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
  75. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  76. // Move up 2 positions.
  77. api::move_category(category_controller::create($id4), $id2);
  78. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  79. $this->assertEquals([$id0, $id1, $id4, $id2, $id3, $id5], array_keys($categories));
  80. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  81. // Move down 2 positions.
  82. api::move_category(category_controller::create($id4), $id5);
  83. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  84. $this->assertEquals([$id0, $id1, $id2, $id3, $id4, $id5], array_keys($categories));
  85. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  86. // Move to the end of the list.
  87. api::move_category(category_controller::create($id2));
  88. $categories = api::get_categories_with_fields($params['component'], $params['area'], $params['itemid']);
  89. $this->assertEquals([$id0, $id1, $id3, $id4, $id5, $id2], array_keys($categories));
  90. $this->assert_property_in_array([0, 1, 2, 3, 4, 5], $categories, 'sortorder');
  91. }
  92. /**
  93. * Tests for \core_customfield\api::get_categories_with_fields() behaviour.
  94. */
  95. public function test_get_categories_with_fields() {
  96. $this->resetAfterTest();
  97. // Create the categories.
  98. $options = [
  99. 'component' => 'core_course',
  100. 'area' => 'course',
  101. 'itemid' => 0,
  102. 'contextid' => \context_system::instance()->id
  103. ];
  104. $category0 = $this->get_generator()->create_category(['name' => 'aaaa'] + $options);
  105. $category1 = $this->get_generator()->create_category(['name' => 'bbbb'] + $options);
  106. $category2 = $this->get_generator()->create_category(['name' => 'cccc'] + $options);
  107. $category3 = $this->get_generator()->create_category(['name' => 'dddd'] + $options);
  108. $category4 = $this->get_generator()->create_category(['name' => 'eeee'] + $options);
  109. $category5 = $this->get_generator()->create_category(['name' => 'ffff'] + $options);
  110. // Let's test counts.
  111. $this->assertCount(6, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  112. api::delete_category($category5);
  113. $this->assertCount(5, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  114. api::delete_category($category4);
  115. $this->assertCount(4, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  116. api::delete_category($category3);
  117. $this->assertCount(3, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  118. api::delete_category($category2);
  119. $this->assertCount(2, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  120. api::delete_category($category1);
  121. $this->assertCount(1, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  122. api::delete_category($category0);
  123. $this->assertCount(0, api::get_categories_with_fields($options['component'], $options['area'], $options['itemid']));
  124. }
  125. /**
  126. * Test for functions api::save_category() and rename_category)
  127. */
  128. public function test_save_category() {
  129. $this->resetAfterTest();
  130. $params = ['component' => 'core_course', 'area' => 'course', 'itemid' => 0, 'name' => 'Cat1',
  131. 'contextid' => \context_system::instance()->id];
  132. $c1 = category_controller::create(0, (object)$params);
  133. api::save_category($c1);
  134. $this->assertNotEmpty($c1->get('id'));
  135. $c1 = category_controller::create($c1->get('id'));
  136. $expected = $params + ['sortorder' => 0, 'id' => $c1->get('id'), 'description' => '', 'descriptionformat' => 0];
  137. $actual = array_intersect_key((array)$c1->to_record(), $expected); // Ignore timecreated, timemodified.
  138. ksort($expected);
  139. ksort($actual);
  140. $this->assertEquals($expected, $actual);
  141. // Create new category and check that the sortorder will be 1.
  142. $params['name'] = 'Cat2';
  143. $c2 = category_controller::create(0, (object)$params);
  144. api::save_category($c2);
  145. $this->assertNotEmpty($c2->get('id'));
  146. $this->assertEquals(1, $c2->get('sortorder'));
  147. $c2 = category_controller::create($c2->get('id'));
  148. $this->assertEquals(1, $c2->get('sortorder'));
  149. // Rename a category.
  150. $c1->set('name', 'Cat3');
  151. $c1->save();
  152. $c1 = category_controller::create($c1->get('id'));
  153. $this->assertEquals('Cat3', $c1->get('name'));
  154. }
  155. /**
  156. * Test for function handler::create_category
  157. */
  158. public function test_create_category() {
  159. $this->resetAfterTest();
  160. $handler = \core_course\customfield\course_handler::create();
  161. $c1id = $handler->create_category();
  162. $c1 = $handler->get_categories_with_fields()[$c1id];
  163. $this->assertEquals('Other fields', $c1->get('name'));
  164. $this->assertEquals($handler->get_component(), $c1->get('component'));
  165. $this->assertEquals($handler->get_area(), $c1->get('area'));
  166. $this->assertEquals($handler->get_itemid(), $c1->get('itemid'));
  167. $this->assertEquals($handler->get_configuration_context()->id, $c1->get('contextid'));
  168. // Generate more categories and make sure they have different names.
  169. $c2id = $handler->create_category();
  170. $c3id = $handler->create_category();
  171. $c2 = $handler->get_categories_with_fields()[$c2id];
  172. $c3 = $handler->get_categories_with_fields()[$c3id];
  173. $this->assertEquals('Other fields 1', $c2->get('name'));
  174. $this->assertEquals('Other fields 2', $c3->get('name'));
  175. }
  176. /**
  177. * Tests for \core_customfield\api::delete_category() behaviour.
  178. */
  179. public function test_delete_category_with_fields() {
  180. $this->resetAfterTest();
  181. global $DB;
  182. // Create two categories with fields and data.
  183. $options = [
  184. 'component' => 'core_course',
  185. 'area' => 'course',
  186. 'itemid' => 0,
  187. 'contextid' => \context_system::instance()->id
  188. ];
  189. $lpg = $this->get_generator();
  190. $course = $this->getDataGenerator()->create_course();
  191. $dataparams = ['instanceid' => $course->id, 'contextid' => \context_course::instance($course->id)->id];
  192. $category0 = $lpg->create_category($options);
  193. $category1 = $lpg->create_category($options);
  194. for ($i = 0; $i < 6; $i++) {
  195. $f = $lpg->create_field(['categoryid' => $category0->get('id')]);
  196. \core_customfield\data_controller::create(0, (object)$dataparams, $f)->save();
  197. $f = $lpg->create_field(['categoryid' => $category1->get('id')]);
  198. \core_customfield\data_controller::create(0, (object)$dataparams, $f)->save();
  199. }
  200. // Check that each category have fields and store ids for future checks.
  201. list($category0, $category1) = array_values(api::get_categories_with_fields($options['component'],
  202. $options['area'], $options['itemid']));
  203. $category0fieldsids = array_keys($category0->get_fields());
  204. $category1fieldsids = array_keys($category1->get_fields());
  205. // There are 6 records in field table and 6 records in data table for each category.
  206. list($sql, $p) = $DB->get_in_or_equal($category0fieldsids);
  207. $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
  208. $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
  209. list($sql, $p) = $DB->get_in_or_equal($category1fieldsids);
  210. $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
  211. $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
  212. // Delete one category.
  213. $this->assertTrue($category0->get_handler()->delete_category($category0));
  214. // Check that the category fields and data were deleted.
  215. list($sql, $p) = $DB->get_in_or_equal($category0fieldsids);
  216. $this->assertEmpty($DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
  217. $this->assertEmpty($DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
  218. // Check that fields and data for the other category remain.
  219. list($sql, $p) = $DB->get_in_or_equal($category1fieldsids);
  220. $this->assertCount(6, $DB->get_records_select(\core_customfield\field::TABLE, 'id '.$sql, $p));
  221. $this->assertCount(6, $DB->get_records_select(\core_customfield\data::TABLE, 'fieldid '.$sql, $p));
  222. }
  223. }