Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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