Merge branch 'MDL-66230' of https://github.com/paulholden/moodle
authorAdrian Greeve <abgreeve@gmail.com>
Wed, 28 Aug 2019 06:54:36 +0000 (14:54 +0800)
committerAdrian Greeve <abgreeve@gmail.com>
Wed, 28 Aug 2019 06:54:36 +0000 (14:54 +0800)
admin/tool/usertours/classes/privacy/provider.php
admin/tool/usertours/tests/privacy_provider_test.php

index 4441ba8..3713d07 100644 (file)
@@ -77,18 +77,22 @@ class provider implements
             }
 
             if ($descriptionidentifier !== null) {
-                $time = transform::datetime($value);
-                $tour = \tool_usertours\tour::instance($tourid);
+                try {
+                    $tour = \tool_usertours\tour::instance($tourid);
+                    $time = transform::datetime($value);
 
-                writer::export_user_preference(
-                    'tool_usertours',
-                    $name,
-                    $time,
-                    get_string($descriptionidentifier, 'tool_usertours', (object) [
-                        'name' => $tour->get_name(),
-                        'time' => $time,
-                    ])
-                );
+                    writer::export_user_preference(
+                        'tool_usertours',
+                        $name,
+                        $time,
+                        get_string($descriptionidentifier, 'tool_usertours', (object) [
+                            'name' => $tour->get_name(),
+                            'time' => $time,
+                        ])
+                    );
+                } catch (\dml_missing_record_exception $ex) {
+                    // The tour related to this user preference no longer exists.
+                }
             }
         }
     }
index 8cfc81a..db0139d 100644 (file)
@@ -112,4 +112,37 @@ class tool_usertours_privacy_testcase extends \core_privacy\tests\provider_testc
 
         $this->assertCount(2, (array) $prefs);
     }
+
+    /**
+     * Ensure that export_user_preferences excludes deleted tours.
+     */
+    public function test_export_user_preferences_deleted_tour() {
+        global $DB;
+
+        $this->resetAfterTest();
+        $this->setAdminUser();
+
+        $user = \core_user::get_user_by_username('admin');
+
+        $alltours = $DB->get_records('tool_usertours_tours');
+
+        $tour1 = tour::instance(array_shift($alltours)->id);
+        $tour1->mark_user_completed();
+
+        $tour2 = tour::instance(array_shift($alltours)->id);
+        $tour2->mark_user_completed();
+        $tour2->remove();
+
+        $writer = writer::with_context(\context_system::instance());
+
+        provider::export_user_preferences($user->id);
+        $this->assertTrue($writer->has_any_data());
+
+        // We should have one preference.
+        $prefs = $writer->get_user_preferences('tool_usertours');
+        $this->assertCount(1, (array) $prefs);
+
+        // The preference should be related to the first tour.
+        $this->assertContains($tour1->get_name(), reset($prefs)->description);
+    }
 }