MDL-67262 core_course: Unit and behat tests for recommendations.
authorAdrian Greeve <abgreeve@gmail.com>
Mon, 10 Feb 2020 02:14:18 +0000 (10:14 +0800)
committerJake Dallimore <jake@moodle.com>
Mon, 24 Feb 2020 03:00:44 +0000 (11:00 +0800)
course/tests/behat/recommend_activities.feature [new file with mode: 0644]
course/tests/externallib_test.php
course/tests/services_content_item_service_test.php

diff --git a/course/tests/behat/recommend_activities.feature b/course/tests/behat/recommend_activities.feature
new file mode 100644 (file)
index 0000000..20f3e21
--- /dev/null
@@ -0,0 +1,28 @@
+@core @core_course @javascript
+Feature: Recommending activities
+  As an admin I want to recommend activities in the activity chooser
+
+  Scenario: As an admin I can recommend activities from an admin setting page.
+    Given I log in as "admin"
+    And I am on site homepage
+    And I navigate to "Courses > Activity chooser" in site administration
+    And I click on ".activity-recommend-checkbox" "css" in the "Assignment" "table_row"
+    And I navigate to "Courses > Add a new course" in site administration
+    When I navigate to "Courses > Activity chooser" in site administration
+    Then "input[aria-label=\"Recommend activity: Assignment\"][checked=checked]" "css_element" should exist
+    And "input[aria-label=\"Recommend activity: Book\"]:not([checked=checked])" "css_element" should exist
+
+  Scenario: As an admin I can remove recommend activities from an admin setting page.
+    Given I log in as "admin"
+    And I am on site homepage
+    And I navigate to "Courses > Activity chooser" in site administration
+    And I click on ".activity-recommend-checkbox" "css" in the "Assignment" "table_row"
+    And I navigate to "Courses > Add a new course" in site administration
+    And I navigate to "Courses > Activity chooser" in site administration
+    And "input[aria-label=\"Recommend activity: Assignment\"][checked=checked]" "css_element" should exist
+    And "input[aria-label=\"Recommend activity: Book\"]:not([checked=checked])" "css_element" should exist
+    And I click on ".activity-recommend-checkbox" "css" in the "Assignment" "table_row"
+    And I navigate to "Courses > Add a new course" in site administration
+    When I navigate to "Courses > Activity chooser" in site administration
+    Then "input[aria-label=\"Recommend activity: Assignment\"]:not([checked=checked])" "css_element" should exist
+    And "input[aria-label=\"Recommend activity: Book\"]:not([checked=checked])" "css_element" should exist
\ No newline at end of file
index 82811be..c1877c1 100644 (file)
@@ -3170,4 +3170,32 @@ class core_course_externallib_testcase extends externallib_advanced_testcase {
 
         $this->assertEmpty($result['content_items']);
     }
+
+    /**
+     * Test toggling the recommendation of an activity.
+     */
+    public function test_toggle_activity_recommendation() {
+        global $CFG;
+
+        $this->resetAfterTest();
+
+        $context = context_system::instance();
+        $usercontext = context_user::instance($CFG->siteguest);
+        $component = 'core_course';
+        $favouritefactory = \core_favourites\service_factory::get_service_for_user_context($usercontext);
+
+        $areaname = 'test_core';
+        $areaid = 3;
+
+        // Test we have the favourite.
+        $this->setAdminUser();
+        $result = core_course_external::toggle_activity_recommendation($areaname, $areaid);
+        $this->assertTrue($favouritefactory->favourite_exists($component,
+                \core_course\local\service\content_item_service::RECOMMENDATION_PREFIX . $areaname, $areaid, $context));
+        $this->assertTrue($result['status']);
+        // Test that it is now gone.
+        $result = core_course_external::toggle_activity_recommendation($areaname, $areaid);
+        $this->assertFalse($favouritefactory->favourite_exists($component, $areaname, $areaid, $context));
+        $this->assertFalse($result['status']);
+    }
 }
index 2e7d2a4..32e76ce 100644 (file)
@@ -195,4 +195,34 @@ class services_content_item_service_testcase extends \advanced_testcase {
         $this->assertFalse($allitemsassign->favourite);
         $this->assertFalse($courseitemsassign->favourite);
     }
+
+    /**
+     * Test that toggling a recommendation works as anticipated.
+     */
+    public function test_toggle_recommendation() {
+        $this->resetAfterTest();
+
+        // Create a user in a course.
+        $course = $this->getDataGenerator()->create_course();
+        $user = $this->getDataGenerator()->create_and_enrol($course, 'editingteacher');
+        $cis = new content_item_service(new content_item_readonly_repository());
+
+        // Grab a the assign content item, which we'll recommend for the user.
+        $items = $cis->get_all_content_items($user);
+        $assign = $items[array_search('assign', array_column($items, 'name'))];
+        $result = $cis->toggle_recommendation($assign->componentname, $assign->id);
+        $this->assertTrue($result);
+
+        $courseitems = $cis->get_all_content_items($user);
+        $courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
+        $this->assertTrue($courseitemsassign->recommended);
+
+        // Let's toggle the recommendation off.
+        $result = $cis->toggle_recommendation($assign->componentname, $assign->id);
+        $this->assertFalse($result);
+
+        $courseitems = $cis->get_all_content_items($user);
+        $courseitemsassign = $courseitems[array_search('assign', array_column($courseitems, 'name'))];
+        $this->assertFalse($courseitemsassign->recommended);
+    }
 }