Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

   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  
  17  namespace mod_forum;
  18  
  19  use mod_forum_tests_generator_trait;
  20  
  21  defined('MOODLE_INTERNAL') || die();
  22  
  23  require_once (__DIR__ . '/generator_trait.php');
  24  
  25  /**
  26   * The post_attachment vault tests.
  27   *
  28   * @package    mod_forum
  29   * @copyright  2019 Ryan Wyllie <ryan@moodle.com>
  30   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class vaults_post_attachment_test extends \advanced_testcase {
  33      // Make use of the test generator trait.
  34      use mod_forum_tests_generator_trait;
  35  
  36      /**
  37       * Test get_attachments_for_posts.
  38       */
  39      public function test_get_attachments_for_posts() {
  40          $this->resetAfterTest();
  41  
  42          $filestorage = get_file_storage();
  43          $entityfactory = \mod_forum\local\container::get_entity_factory();
  44          $vaultfactory = \mod_forum\local\container::get_vault_factory();
  45          $vault = $vaultfactory->get_post_attachment_vault();
  46          $datagenerator = $this->getDataGenerator();
  47          $user = $datagenerator->create_user();
  48          $course = $datagenerator->create_course();
  49          $forum = $datagenerator->create_module('forum', ['course' => $course->id]);
  50          $coursemodule = get_coursemodule_from_instance('forum', $forum->id);
  51          $context = \context_module::instance($coursemodule->id);
  52          [$discussion, $post1] = $this->helper_post_to_forum($forum, $user);
  53          $post2 = $this->helper_reply_to_post($post1, $user);
  54          $post3 = $this->helper_reply_to_post($post1, $user);
  55          $attachment1 = $filestorage->create_file_from_string(
  56              [
  57                  'contextid' => $context->id,
  58                  'component' => 'mod_forum',
  59                  'filearea'  => 'attachment',
  60                  'itemid'    => $post1->id,
  61                  'filepath'  => '/',
  62                  'filename'  => 'example1.jpg',
  63              ],
  64              'image contents'
  65          );
  66          $attachment2 = $filestorage->create_file_from_string(
  67              [
  68                  'contextid' => $context->id,
  69                  'component' => 'mod_forum',
  70                  'filearea'  => 'attachment',
  71                  'itemid'    => $post2->id,
  72                  'filepath'  => '/',
  73                  'filename'  => 'example2.jpg',
  74              ],
  75              'image contents'
  76          );
  77  
  78          $post1 = $entityfactory->get_post_from_stdClass($post1);
  79          $post2 = $entityfactory->get_post_from_stdClass($post2);
  80          $post3 = $entityfactory->get_post_from_stdClass($post3);
  81  
  82          $results = $vault->get_attachments_for_posts(\context_system::instance(), [$post1, $post2, $post3]);
  83          $this->assertCount(3, $results);
  84          $this->assertEquals([], $results[$post1->get_id()]);
  85          $this->assertEquals([], $results[$post2->get_id()]);
  86          $this->assertEquals([], $results[$post3->get_id()]);
  87  
  88          $results = $vault->get_attachments_for_posts($context, [$post1]);
  89          $this->assertCount(1, $results);
  90          $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename());
  91  
  92          $results = $vault->get_attachments_for_posts($context, [$post1, $post2]);
  93          $this->assertCount(2, $results);
  94          $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename());
  95          $this->assertEquals($attachment2->get_filename(), $results[$post2->get_id()][0]->get_filename());
  96  
  97          $results = $vault->get_attachments_for_posts($context, [$post1, $post2, $post3]);
  98          $this->assertCount(3, $results);
  99          $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename());
 100          $this->assertEquals($attachment2->get_filename(), $results[$post2->get_id()][0]->get_filename());
 101          $this->assertEquals([], $results[$post3->get_id()]);
 102      }
 103  }