Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402]
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 104 /** 105 * Test get_inline_attachments_for_posts. 106 */ 107 public function test_get_inline_attachments_for_posts() { 108 $this->resetAfterTest(); 109 110 $filestorage = get_file_storage(); 111 $entityfactory = \mod_forum\local\container::get_entity_factory(); 112 $vaultfactory = \mod_forum\local\container::get_vault_factory(); 113 $vault = $vaultfactory->get_post_attachment_vault(); 114 $datagenerator = $this->getDataGenerator(); 115 $user = $datagenerator->create_user(); 116 $course = $datagenerator->create_course(); 117 $forum = $datagenerator->create_module('forum', ['course' => $course->id]); 118 $coursemodule = get_coursemodule_from_instance('forum', $forum->id); 119 $context = \context_module::instance($coursemodule->id); 120 [$discussion, $post1] = $this->helper_post_to_forum($forum, $user); 121 $post2 = $this->helper_reply_to_post($post1, $user); 122 $post3 = $this->helper_reply_to_post($post1, $user); 123 $attachment1 = $filestorage->create_file_from_string( 124 [ 125 'contextid' => $context->id, 126 'component' => 'mod_forum', 127 'filearea' => 'post', 128 'itemid' => $post1->id, 129 'filepath' => '/', 130 'filename' => 'example1.jpg', 131 ], 132 'image contents' 133 ); 134 $attachment2 = $filestorage->create_file_from_string( 135 [ 136 'contextid' => $context->id, 137 'component' => 'mod_forum', 138 'filearea' => 'post', 139 'itemid' => $post2->id, 140 'filepath' => '/', 141 'filename' => 'example2.jpg', 142 ], 143 'image contents' 144 ); 145 146 $post1 = $entityfactory->get_post_from_stdClass($post1); 147 $post2 = $entityfactory->get_post_from_stdClass($post2); 148 $post3 = $entityfactory->get_post_from_stdClass($post3); 149 150 $results = $vault->get_inline_attachments_for_posts(\context_system::instance(), [$post1, $post2, $post3]); 151 $this->assertCount(3, $results); 152 $this->assertEquals([], $results[$post1->get_id()]); 153 $this->assertEquals([], $results[$post2->get_id()]); 154 $this->assertEquals([], $results[$post3->get_id()]); 155 156 $results = $vault->get_inline_attachments_for_posts($context, [$post1]); 157 $this->assertCount(1, $results); 158 $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename()); 159 160 $results = $vault->get_inline_attachments_for_posts($context, [$post1, $post2]); 161 $this->assertCount(2, $results); 162 $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename()); 163 $this->assertEquals($attachment2->get_filename(), $results[$post2->get_id()][0]->get_filename()); 164 165 $results = $vault->get_inline_attachments_for_posts($context, [$post1, $post2, $post3]); 166 $this->assertCount(3, $results); 167 $this->assertEquals($attachment1->get_filename(), $results[$post1->get_id()][0]->get_filename()); 168 $this->assertEquals($attachment2->get_filename(), $results[$post2->get_id()][0]->get_filename()); 169 $this->assertEquals([], $results[$post3->get_id()]); 170 } 171 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body