MDL-70438 contentbank: Add method get_uses to content
authorSara Arjona <sara@moodle.com>
Wed, 9 Dec 2020 12:34:41 +0000 (13:34 +0100)
committerSara Arjona <sara@moodle.com>
Tue, 19 Jan 2021 07:21:30 +0000 (08:21 +0100)
contentbank/classes/content.php
contentbank/tests/content_test.php
contentbank/upgrade.txt [new file with mode: 0644]

index 227294b..8048afe 100644 (file)
@@ -310,6 +310,24 @@ abstract class content {
         return null;
     }
 
+    /**
+     * Returns the places where the file associated to this content is used or an empty array if the content has no file.
+     *
+     * @return array of stored_file where current file content is used or empty array if it hasn't any file.
+     * @since 3.11
+     */
+    public function get_uses(): ?array {
+        $references = [];
+
+        $file = $this->get_file();
+        if ($file != null) {
+            $fs = get_file_storage();
+            $references = $fs->get_references_by_storedfile($file);
+        }
+
+        return $references;
+    }
+
     /**
      * Returns the file url related to this content.
      *
index 4c7ec2b..7bc2e83 100644 (file)
@@ -298,4 +298,60 @@ class core_contenttype_content_testcase extends \advanced_testcase {
 
         $this->assertInstanceOf(get_class($type), $contenttype);
     }
+
+    /**
+     * Tests for 'get_uses' behaviour.
+     *
+     * @covers ::get_uses
+     */
+    public function test_get_uses() {
+        $this->resetAfterTest();
+        $this->setAdminUser();
+        $context = context_system::instance();
+
+        // Add some content to the content bank.
+        $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
+        $contents = $generator->generate_contentbank_data('contenttype_testable', 3, 0, $context);
+        $content1 = array_shift($contents);
+
+        // Check content has no references for now.
+        $this->assertCount(0, $content1->get_uses());
+
+        // Add a link to the previous content.
+        $cbfile = $content1->get_file();
+        $cbrecord = array(
+            'contextid' => $cbfile->get_contextid(),
+            'component' => $cbfile->get_component(),
+            'filearea'  => $cbfile->get_filearea(),
+            'itemid'    => $cbfile->get_itemid(),
+            'filepath'  => $cbfile->get_filepath(),
+            'filename'  => $cbfile->get_filename(),
+        );
+        $fs = get_file_storage();
+        $ref = $fs->pack_reference($cbrecord);
+
+        $aliasrecord = new stdClass();
+        $aliasrecord->contextid = $context->id;
+        $aliasrecord->component = 'core';
+        $aliasrecord->filearea = 'phpunit';
+        $aliasrecord->filepath = '/foo/';
+        $aliasrecord->filename = 'one.txt';
+        $aliasrecord->itemid = 0;
+
+        $repos = \repository::get_instances(['type' => 'contentbank']);
+        $cbrepo = reset($repos);
+        $this->assertInstanceOf('repository', $cbrepo);
+
+        $alias = $fs->create_file_from_reference($aliasrecord, $cbrepo->id, $ref);
+
+        // Check content now has one reference (the previous alias).
+        $contentuses1 = $content1->get_uses();
+        $this->assertCount(1, $contentuses1);
+        $reffile = reset($contentuses1);
+        $this->assertEquals($alias, $reffile);
+
+        // Check a different content hasn't any reference.
+        $content2 = array_shift($contents);
+        $this->assertCount(0, $content2->get_uses());
+    }
 }
diff --git a/contentbank/upgrade.txt b/contentbank/upgrade.txt
new file mode 100644 (file)
index 0000000..a94fbcf
--- /dev/null
@@ -0,0 +1,5 @@
+This files describes API changes in core libraries and APIs,
+information provided here is intended especially for developers.
+
+=== 3.11 ===
+* Added "get_uses()" method to content class to return places where a content is used.