MDL-67585 core_course: Added legacyitem to exporter.
authorAdrian Greeve <abgreeve@gmail.com>
Tue, 4 Feb 2020 03:31:31 +0000 (11:31 +0800)
committerJake Dallimore <jake@moodle.com>
Thu, 20 Feb 2020 03:42:24 +0000 (11:42 +0800)
Legacy items have no id and so can not be favourited nor
recommended. We needed a way to easily determine whether to show
these features in the templates.

course/classes/local/exporters/course_content_item_exporter.php
course/tests/exporters_content_item_test.php

index cf8d77c..3123418 100644 (file)
@@ -77,7 +77,13 @@ class course_content_item_exporter extends exporter {
      */
     protected static function define_other_properties() {
         // This will hold user-dependant properties such as whether the item is starred or recommended.
-        return ['favourite' => ['type' => PARAM_BOOL, 'description' => 'Has the user favourited the content item']];
+        return [
+            'favourite' => ['type' => PARAM_BOOL, 'description' => 'Has the user favourited the content item'],
+            'legacyitem' => [
+                'type' => PARAM_BOOL,
+                'description' => 'If this item was pulled from the old callback and has no item id.'
+            ]
+        ];
     }
 
     /**
@@ -116,7 +122,8 @@ class course_content_item_exporter extends exporter {
             'help' => $this->contentitem->get_help(),
             'archetype' => $this->contentitem->get_archetype(),
             'componentname' => $this->contentitem->get_component_name(),
-            'favourite' => $favourite
+            'favourite' => $favourite,
+            'legacyitem' => ($this->contentitem->get_id() == -1)
         ];
 
         return $properties;
index 32c0018..872ece6 100644 (file)
@@ -70,5 +70,52 @@ class exporters_course_content_item_testcase extends \advanced_testcase {
         $this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
         $this->assertObjectHasAttribute('componentname', $exporteditem);
         $this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
+        $this->assertObjectHasAttribute('legacyitem', $exporteditem);
+        $this->assertFalse($exporteditem->legacyitem);
+    }
+
+    /**
+     * Test that legacy items (with id of -1) are exported correctly.
+     */
+    public function test_export_course_content_item_legacy() {
+        $this->resetAfterTest();
+        global $PAGE;
+
+        $course = $this->getDataGenerator()->create_course();
+
+        $contentitem = new \core_course\local\entity\content_item(
+            -1,
+            'test_name',
+            new \core_course\local\entity\string_title('test_title'),
+            new \moodle_url(''),
+            '',
+            '',
+            MOD_ARCHETYPE_OTHER,
+            'core_test'
+        );
+
+        $ciexporter = new course_content_item_exporter($contentitem, ['context' => \context_course::instance($course->id)]);
+        $renderer = $PAGE->get_renderer('core');
+        $exporteditem = $ciexporter->export($renderer);
+
+        $this->assertObjectHasAttribute('id', $exporteditem);
+        $this->assertEquals($exporteditem->id, $contentitem->get_id());
+        $this->assertObjectHasAttribute('name', $exporteditem);
+        $this->assertEquals($exporteditem->name, $contentitem->get_name());
+        $this->assertObjectHasAttribute('title', $exporteditem);
+        $this->assertEquals($exporteditem->title, $contentitem->get_title()->get_value());
+        $this->assertObjectHasAttribute('link', $exporteditem);
+        $this->assertEquals($exporteditem->link, $contentitem->get_link()->out(false));
+        $this->assertObjectHasAttribute('icon', $exporteditem);
+        $this->assertEquals($exporteditem->icon, $contentitem->get_icon());
+        $this->assertObjectHasAttribute('help', $exporteditem);
+        $this->assertEquals($exporteditem->help, $contentitem->get_help());
+        $this->assertObjectHasAttribute('archetype', $exporteditem);
+        $this->assertEquals($exporteditem->archetype, $contentitem->get_archetype());
+        $this->assertObjectHasAttribute('componentname', $exporteditem);
+        $this->assertEquals($exporteditem->componentname, $contentitem->get_component_name());
+        // Most important, is this a legacy item?
+        $this->assertObjectHasAttribute('legacyitem', $exporteditem);
+        $this->assertTrue($exporteditem->legacyitem);
     }
 }