| 1 | 
           efrain | 
           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 | 
            * Unit tests for blog external API.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    core_blog
  | 
        
        
            | 
            | 
           21 | 
            * @copyright  2018 Juan Leyva
  | 
        
        
            | 
            | 
           22 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           namespace core_blog\external;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           use core_external\external_api;
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
            | 
            | 
           29 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           30 | 
              | 
        
        
            | 
            | 
           31 | 
           global $CFG;
  | 
        
        
            | 
            | 
           32 | 
           require_once($CFG->dirroot . '/blog/locallib.php');
  | 
        
        
            | 
            | 
           33 | 
           require_once($CFG->dirroot . '/blog/lib.php');
  | 
        
        
            | 
            | 
           34 | 
           require_once($CFG->dirroot . '/webservice/tests/helpers.php');
  | 
        
        
            | 
            | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
           /**
  | 
        
        
            | 
            | 
           37 | 
            * Unit tests for blog external API.
  | 
        
        
            | 
            | 
           38 | 
            *
  | 
        
        
            | 
            | 
           39 | 
            * @package    core_blog
  | 
        
        
            | 
            | 
           40 | 
            * @copyright  2018 Juan Leyva
  | 
        
        
            | 
            | 
           41 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           42 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           43 | 
           final class external_test extends \externallib_advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           44 | 
              | 
        
        
            | 
            | 
           45 | 
               private $courseid;
  | 
        
        
            | 
            | 
           46 | 
               private $cmid;
  | 
        
        
            | 
            | 
           47 | 
               private $userid;
  | 
        
        
            | 
            | 
           48 | 
               private $groupid;
  | 
        
        
            | 
            | 
           49 | 
               private $tagid;
  | 
        
        
            | 
            | 
           50 | 
               private $postid;
  | 
        
        
            | 
            | 
           51 | 
              | 
        
        
            | 
            | 
           52 | 
               /** @var string publish state. */
  | 
        
        
            | 
            | 
           53 | 
               protected $publishstate;
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
               protected function setUp(): void {
  | 
        
        
            | 
            | 
           56 | 
                   global $DB, $CFG;
  | 
        
        
            | 
            | 
           57 | 
                   parent::setUp();
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           60 | 
              | 
        
        
            | 
            | 
           61 | 
                   // Create default course.
  | 
        
        
            | 
            | 
           62 | 
                   $course = $this->getDataGenerator()->create_course(array('category' => 1, 'shortname' => 'ANON'));
  | 
        
        
            | 
            | 
           63 | 
                   $this->assertNotEmpty($course);
  | 
        
        
            | 
            | 
           64 | 
                   $page = $this->getDataGenerator()->create_module('page', array('course' => $course->id));
  | 
        
        
            | 
            | 
           65 | 
                   $this->assertNotEmpty($page);
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   // Create default user.
  | 
        
        
            | 
            | 
           68 | 
                   $user = $this->getDataGenerator()->create_user(array(
  | 
        
        
            | 
            | 
           69 | 
                           'username' => 'testuser',
  | 
        
        
            | 
            | 
           70 | 
                           'firstname' => 'Jimmy',
  | 
        
        
            | 
            | 
           71 | 
                           'lastname' => 'Kinnon'
  | 
        
        
            | 
            | 
           72 | 
                   ));
  | 
        
        
            | 
            | 
           73 | 
                   // Enrol user.
  | 
        
        
            | 
            | 
           74 | 
                   $this->getDataGenerator()->enrol_user($user->id, $course->id);
  | 
        
        
            | 
            | 
           75 | 
              | 
        
        
            | 
            | 
           76 | 
                   $group = $this->getDataGenerator()->create_group(array('courseid' => $course->id));
  | 
        
        
            | 
            | 
           77 | 
                   $this->getDataGenerator()->create_group_member(array('groupid' => $group->id, 'userid' => $user->id));
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
                   // Create default post.
  | 
        
        
            | 
            | 
           80 | 
                   $post = new \stdClass();
  | 
        
        
            | 
            | 
           81 | 
                   $post->userid = $user->id;
  | 
        
        
            | 
            | 
           82 | 
                   $post->courseid = $course->id;
  | 
        
        
            | 
            | 
           83 | 
                   $post->groupid = $group->id;
  | 
        
        
            | 
            | 
           84 | 
                   $post->content = 'test post content text';
  | 
        
        
            | 
            | 
           85 | 
                   $post->module = 'blog';
  | 
        
        
            | 
            | 
           86 | 
                   $post->id = $DB->insert_record('post', $post);
  | 
        
        
            | 
            | 
           87 | 
              | 
        
        
            | 
            | 
           88 | 
                   \core_tag_tag::set_item_tags('core', 'post', $post->id, \context_user::instance($user->id), array('tag1'));
  | 
        
        
            | 
            | 
           89 | 
                   $tagid = $DB->get_field('tag', 'id', array('name' => 'tag1'));
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   // Grab important ids.
  | 
        
        
            | 
            | 
           92 | 
                   $this->courseid = $course->id;
  | 
        
        
            | 
            | 
           93 | 
                   $this->cmid = $page->cmid;
  | 
        
        
            | 
            | 
           94 | 
                   $this->userid  = $user->id;
  | 
        
        
            | 
            | 
           95 | 
                   $this->groupid  = $group->id;
  | 
        
        
            | 
            | 
           96 | 
                   $this->tagid  = $tagid;
  | 
        
        
            | 
            | 
           97 | 
                   $this->postid = $post->id;
  | 
        
        
            | 
            | 
           98 | 
                   $this->publishstate = 'site';   // To be override in tests.
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
                   // Set default blog level.
  | 
        
        
            | 
            | 
           101 | 
                   $CFG->bloglevel = BLOG_SITE_LEVEL;
  | 
        
        
            | 
            | 
           102 | 
               }
  | 
        
        
            | 
            | 
           103 | 
              | 
        
        
            | 
            | 
           104 | 
               /**
  | 
        
        
            | 
            | 
           105 | 
                * Get global public entries even for not authenticated users.
  | 
        
        
            | 
            | 
           106 | 
                * We get the entry since is public.
  | 
        
        
            | 
            | 
           107 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           108 | 
               public function test_get_public_entries_global_level_by_non_logged_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           109 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                   $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
  | 
        
        
            | 
            | 
           112 | 
                   $CFG->forcelogin = 0;
  | 
        
        
            | 
            | 
           113 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           114 | 
                   $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           115 | 
              | 
        
        
            | 
            | 
           116 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           117 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           118 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           119 | 
                   $this->assertCount(1, $result['entries'][0]['tags']);
  | 
        
        
            | 
            | 
           120 | 
                   $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
  | 
        
        
            | 
            | 
           121 | 
              | 
        
        
            | 
            | 
           122 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           123 | 
                   $this->assertFalse($result['entries'][0]['canedit']);
  | 
        
        
            | 
            | 
           124 | 
               }
  | 
        
        
            | 
            | 
           125 | 
              | 
        
        
            | 
            | 
           126 | 
               /**
  | 
        
        
            | 
            | 
           127 | 
                * Get global public entries even for not authenticated users in closed site.
  | 
        
        
            | 
            | 
           128 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           129 | 
               public function test_get_public_entries_global_level_by_non_logged_users_closed_site(): void {
  | 
        
        
           | 1 | 
           efrain | 
           130 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
                   $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
  | 
        
        
            | 
            | 
           133 | 
                   $CFG->forcelogin = 1;
  | 
        
        
            | 
            | 
           134 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           135 | 
                   $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           138 | 
                   \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           139 | 
               }
  | 
        
        
            | 
            | 
           140 | 
              | 
        
        
            | 
            | 
           141 | 
               /**
  | 
        
        
            | 
            | 
           142 | 
                * Get global public entries for guest users.
  | 
        
        
            | 
            | 
           143 | 
                * We get the entry since is public.
  | 
        
        
            | 
            | 
           144 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           145 | 
               public function test_get_public_entries_global_level_by_guest_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           146 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           147 | 
              | 
        
        
            | 
            | 
           148 | 
                   $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
  | 
        
        
            | 
            | 
           149 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           150 | 
                   $DB->set_field('post', 'publishstate', 'public', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           151 | 
              | 
        
        
            | 
            | 
           152 | 
                   $this->setGuestUser();
  | 
        
        
            | 
            | 
           153 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           154 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           155 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           156 | 
                   $this->assertCount(1, $result['entries'][0]['tags']);
  | 
        
        
            | 
            | 
           157 | 
                   $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
  | 
        
        
            | 
            | 
           158 | 
              | 
        
        
            | 
            | 
           159 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           160 | 
               }
  | 
        
        
            | 
            | 
           161 | 
              | 
        
        
            | 
            | 
           162 | 
               /**
  | 
        
        
            | 
            | 
           163 | 
                * Get global not public entries even for not authenticated users withouth being authenticated.
  | 
        
        
            | 
            | 
           164 | 
                * We don't get any because they are not public (restricted to site users).
  | 
        
        
            | 
            | 
           165 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           166 | 
               public function test_get_not_public_entries_global_level_by_non_logged_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           167 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           168 | 
              | 
        
        
            | 
            | 
           169 | 
                   $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
  | 
        
        
            | 
            | 
           170 | 
              | 
        
        
            | 
            | 
           171 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           172 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           173 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           174 | 
               }
  | 
        
        
            | 
            | 
           175 | 
              | 
        
        
            | 
            | 
           176 | 
               /**
  | 
        
        
            | 
            | 
           177 | 
                * Get global not public entries users being guest.
  | 
        
        
            | 
            | 
           178 | 
                * We don't get any because they are not public (restricted to real site users).
  | 
        
        
            | 
            | 
           179 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           180 | 
               public function test_get_not_public_entries_global_level_by_guest_user(): void {
  | 
        
        
           | 1 | 
           efrain | 
           181 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           182 | 
              | 
        
        
            | 
            | 
           183 | 
                   $CFG->bloglevel = BLOG_GLOBAL_LEVEL;
  | 
        
        
            | 
            | 
           184 | 
              | 
        
        
            | 
            | 
           185 | 
                   $this->setGuestUser();
  | 
        
        
            | 
            | 
           186 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           187 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           188 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           189 | 
               }
  | 
        
        
            | 
            | 
           190 | 
              | 
        
        
            | 
            | 
           191 | 
               /**
  | 
        
        
            | 
            | 
           192 | 
                * Get site not public entries for not authenticated users.
  | 
        
        
            | 
            | 
           193 | 
                * We don't get any because they are not public (restricted to real site users).
  | 
        
        
            | 
            | 
           194 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           195 | 
               public function test_get_not_public_entries_site_level_by_non_logged_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           196 | 
                   $this->expectException('require_login_exception'); // In this case we get a security exception.
  | 
        
        
            | 
            | 
           197 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           198 | 
               }
  | 
        
        
            | 
            | 
           199 | 
              | 
        
        
            | 
            | 
           200 | 
               /**
  | 
        
        
            | 
            | 
           201 | 
                * Get site not public entries for guest users.
  | 
        
        
            | 
            | 
           202 | 
                * We don't get any because they are not public (restricted to real site users).
  | 
        
        
            | 
            | 
           203 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           204 | 
               public function test_get_not_public_entries_site_level_by_guest_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
                   $this->setGuestUser();
  | 
        
        
            | 
            | 
           207 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           208 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           209 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           210 | 
               }
  | 
        
        
            | 
            | 
           211 | 
              | 
        
        
            | 
            | 
           212 | 
               /**
  | 
        
        
            | 
            | 
           213 | 
                * Get site entries at site level by system users.
  | 
        
        
            | 
            | 
           214 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           215 | 
               public function test_get_site_entries_site_level_by_normal_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           216 | 
              | 
        
        
            | 
            | 
           217 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           218 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           219 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           220 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           221 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           222 | 
               }
  | 
        
        
            | 
            | 
           223 | 
              | 
        
        
            | 
            | 
           224 | 
               /**
  | 
        
        
            | 
            | 
           225 | 
                * Get draft site entries by authors.
  | 
        
        
            | 
            | 
           226 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           227 | 
               public function test_get_draft_entries_site_level_by_author_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           228 | 
                   global $DB;
  | 
        
        
            | 
            | 
           229 | 
              | 
        
        
            | 
            | 
           230 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           231 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           232 | 
              | 
        
        
            | 
            | 
           233 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           234 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           235 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           236 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           237 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           238 | 
               }
  | 
        
        
            | 
            | 
           239 | 
              | 
        
        
            | 
            | 
           240 | 
               /**
  | 
        
        
            | 
            | 
           241 | 
                * Get draft site entries by not authors.
  | 
        
        
            | 
            | 
           242 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           243 | 
               public function test_get_draft_entries_site_level_by_not_author_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           244 | 
                   global $DB;
  | 
        
        
            | 
            | 
           245 | 
              | 
        
        
            | 
            | 
           246 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           247 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           248 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           249 | 
              | 
        
        
            | 
            | 
           250 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           251 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           252 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           253 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           254 | 
               }
  | 
        
        
            | 
            | 
           255 | 
              | 
        
        
            | 
            | 
           256 | 
               /**
  | 
        
        
            | 
            | 
           257 | 
                * Get draft site entries by admin.
  | 
        
        
            | 
            | 
           258 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           259 | 
               public function test_get_draft_entries_site_level_by_admin_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           260 | 
                   global $DB;
  | 
        
        
            | 
            | 
           261 | 
              | 
        
        
            | 
            | 
           262 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           263 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           264 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           265 | 
              | 
        
        
            | 
            | 
           266 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           267 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           268 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           269 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           270 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           271 | 
                   $this->assertTrue($result['entries'][0]['canedit']);
  | 
        
        
            | 
            | 
           272 | 
               }
  | 
        
        
            | 
            | 
           273 | 
              | 
        
        
            | 
            | 
           274 | 
               /**
  | 
        
        
            | 
            | 
           275 | 
                * Get draft user entries by authors.
  | 
        
        
            | 
            | 
           276 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           277 | 
               public function test_get_draft_entries_user_level_by_author_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           278 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           279 | 
              | 
        
        
            | 
            | 
           280 | 
                   $CFG->bloglevel = BLOG_USER_LEVEL;
  | 
        
        
            | 
            | 
           281 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           282 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           283 | 
              | 
        
        
            | 
            | 
           284 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           285 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           286 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           287 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           288 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           289 | 
                   $this->assertTrue($result['entries'][0]['canedit']);
  | 
        
        
            | 
            | 
           290 | 
               }
  | 
        
        
            | 
            | 
           291 | 
              | 
        
        
            | 
            | 
           292 | 
               /**
  | 
        
        
            | 
            | 
           293 | 
                * Get draft user entries by not authors.
  | 
        
        
            | 
            | 
           294 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           295 | 
               public function test_get_draft_entries_user_level_by_not_author_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           296 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           297 | 
              | 
        
        
            | 
            | 
           298 | 
                   $CFG->bloglevel = BLOG_USER_LEVEL;
  | 
        
        
            | 
            | 
           299 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           300 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           301 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           302 | 
              | 
        
        
            | 
            | 
           303 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           304 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           305 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           306 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           307 | 
               }
  | 
        
        
            | 
            | 
           308 | 
              | 
        
        
            | 
            | 
           309 | 
               /**
  | 
        
        
            | 
            | 
           310 | 
                * Get draft user entries by admin.
  | 
        
        
            | 
            | 
           311 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           312 | 
               public function test_get_draft_entries_user_level_by_admin_users(): void {
  | 
        
        
           | 1 | 
           efrain | 
           313 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           314 | 
              | 
        
        
            | 
            | 
           315 | 
                   $CFG->bloglevel = BLOG_USER_LEVEL;
  | 
        
        
            | 
            | 
           316 | 
                   // Set current entry global.
  | 
        
        
            | 
            | 
           317 | 
                   $DB->set_field('post', 'publishstate', 'draft', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           318 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           319 | 
              | 
        
        
            | 
            | 
           320 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           321 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           322 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           323 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           324 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           325 | 
               }
  | 
        
        
            | 
            | 
           326 | 
              | 
        
        
            | 
            | 
           327 | 
               /**
  | 
        
        
            | 
            | 
           328 | 
                * Test get all entries including testing pagination.
  | 
        
        
            | 
            | 
           329 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           330 | 
               public function test_get_all_entries_including_pagination(): void {
  | 
        
        
           | 1 | 
           efrain | 
           331 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           332 | 
              | 
        
        
            | 
            | 
           333 | 
                   $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           334 | 
              | 
        
        
            | 
            | 
           335 | 
                   // Create another entry.
  | 
        
        
            | 
            | 
           336 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           337 | 
                   $newpost = new \stdClass();
  | 
        
        
            | 
            | 
           338 | 
                   $newpost->userid = $USER->id;
  | 
        
        
            | 
            | 
           339 | 
                   $newpost->content = 'test post content text';
  | 
        
        
            | 
            | 
           340 | 
                   $newpost->module = 'blog';
  | 
        
        
            | 
            | 
           341 | 
                   $newpost->publishstate = 'site';
  | 
        
        
            | 
            | 
           342 | 
                   $newpost->created = time() + HOURSECS;
  | 
        
        
            | 
            | 
           343 | 
                   $newpost->lastmodified = time() + HOURSECS;
  | 
        
        
            | 
            | 
           344 | 
                   $newpost->id = $DB->insert_record('post', $newpost);
  | 
        
        
            | 
            | 
           345 | 
              | 
        
        
            | 
            | 
           346 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           347 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           348 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           349 | 
                   $this->assertCount(2, $result['entries']);
  | 
        
        
            | 
            | 
           350 | 
                   $this->assertEquals(2, $result['totalentries']);
  | 
        
        
            | 
            | 
           351 | 
                   $this->assertCount(0, $result['entries'][0]['tags']);
  | 
        
        
            | 
            | 
           352 | 
                   $this->assertCount(1, $result['entries'][1]['tags']);
  | 
        
        
            | 
            | 
           353 | 
                   $this->assertEquals('tag1', $result['entries'][1]['tags'][0]['rawname']);
  | 
        
        
            | 
            | 
           354 | 
              | 
        
        
            | 
            | 
           355 | 
                   $result = \core_blog\external::get_entries(array(), 0, 1);
  | 
        
        
            | 
            | 
           356 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           357 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           358 | 
                   $this->assertEquals(2, $result['totalentries']);
  | 
        
        
            | 
            | 
           359 | 
                   $this->assertEquals($newpost->id, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           360 | 
              | 
        
        
            | 
            | 
           361 | 
                   $result = \core_blog\external::get_entries(array(), 1, 1);
  | 
        
        
            | 
            | 
           362 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           363 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           364 | 
                   $this->assertEquals(2, $result['totalentries']);
  | 
        
        
            | 
            | 
           365 | 
                   $this->assertEquals($this->postid, $result['entries'][0]['id']);
  | 
        
        
            | 
            | 
           366 | 
               }
  | 
        
        
            | 
            | 
           367 | 
              | 
        
        
            | 
            | 
           368 | 
               /**
  | 
        
        
            | 
            | 
           369 | 
                * Test get entries filtering by course.
  | 
        
        
            | 
            | 
           370 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           371 | 
               public function test_get_entries_filtering_by_course(): void {
  | 
        
        
           | 1 | 
           efrain | 
           372 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           373 | 
              | 
        
        
            | 
            | 
           374 | 
                   $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           375 | 
              | 
        
        
            | 
            | 
           376 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           377 | 
                   $coursecontext = \context_course::instance($this->courseid);
  | 
        
        
            | 
            | 
           378 | 
                   $anothercourse = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           379 | 
              | 
        
        
            | 
            | 
           380 | 
                   // Add blog associations with a course.
  | 
        
        
            | 
            | 
           381 | 
                   $blog = new \blog_entry($this->postid);
  | 
        
        
            | 
            | 
           382 | 
                   $blog->add_association($coursecontext->id);
  | 
        
        
            | 
            | 
           383 | 
              | 
        
        
            | 
            | 
           384 | 
                   // There is one entry associated with a course.
  | 
        
        
            | 
            | 
           385 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $this->courseid)));
  | 
        
        
            | 
            | 
           386 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           387 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           388 | 
                   $this->assertCount(1, $result['entries'][0]['tags']);
  | 
        
        
            | 
            | 
           389 | 
                   $this->assertEquals('tag1', $result['entries'][0]['tags'][0]['rawname']);
  | 
        
        
            | 
            | 
           390 | 
              | 
        
        
            | 
            | 
           391 | 
                   // There is no entry associated with a wrong course.
  | 
        
        
            | 
            | 
           392 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $anothercourse->id)));
  | 
        
        
            | 
            | 
           393 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           394 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           395 | 
              | 
        
        
            | 
            | 
           396 | 
                   // There is no entry associated with a module.
  | 
        
        
            | 
            | 
           397 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $this->cmid)));
  | 
        
        
            | 
            | 
           398 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           399 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           400 | 
               }
  | 
        
        
            | 
            | 
           401 | 
              | 
        
        
            | 
            | 
           402 | 
               /**
  | 
        
        
            | 
            | 
           403 | 
                * Test get entries filtering by module.
  | 
        
        
            | 
            | 
           404 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           405 | 
               public function test_get_entries_filtering_by_module(): void {
  | 
        
        
           | 1 | 
           efrain | 
           406 | 
                   global $CFG, $DB;
  | 
        
        
            | 
            | 
           407 | 
              | 
        
        
            | 
            | 
           408 | 
                   $DB->set_field('post', 'publishstate', 'site', array('id' => $this->postid));
  | 
        
        
            | 
            | 
           409 | 
              | 
        
        
            | 
            | 
           410 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           411 | 
                   $coursecontext = \context_course::instance($this->courseid);
  | 
        
        
            | 
            | 
           412 | 
                   $contextmodule = \context_module::instance($this->cmid);
  | 
        
        
            | 
            | 
           413 | 
                   $anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));
  | 
        
        
            | 
            | 
           414 | 
              | 
        
        
            | 
            | 
           415 | 
                   // Add blog associations with a module.
  | 
        
        
            | 
            | 
           416 | 
                   $blog = new \blog_entry($this->postid);
  | 
        
        
            | 
            | 
           417 | 
                   $blog->add_association($contextmodule->id);
  | 
        
        
            | 
            | 
           418 | 
              | 
        
        
            | 
            | 
           419 | 
                   // There is no entry associated with a course.
  | 
        
        
            | 
            | 
           420 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'courseid', 'value' => $this->courseid)));
  | 
        
        
            | 
            | 
           421 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           422 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           423 | 
              | 
        
        
            | 
            | 
           424 | 
                   // There is one entry associated with a module.
  | 
        
        
            | 
            | 
           425 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $this->cmid)));
  | 
        
        
            | 
            | 
           426 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           427 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           428 | 
              | 
        
        
            | 
            | 
           429 | 
                   // There is no entry associated with a wrong module.
  | 
        
        
            | 
            | 
           430 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'cmid', 'value' => $anothermodule->cmid)));
  | 
        
        
            | 
            | 
           431 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           432 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           433 | 
               }
  | 
        
        
            | 
            | 
           434 | 
              | 
        
        
            | 
            | 
           435 | 
               /**
  | 
        
        
            | 
            | 
           436 | 
                * Test get entries filtering by author.
  | 
        
        
            | 
            | 
           437 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           438 | 
               public function test_get_entries_filtering_by_author(): void {
  | 
        
        
           | 1 | 
           efrain | 
           439 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           440 | 
                   // Filter by author.
  | 
        
        
            | 
            | 
           441 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'userid', 'value' => $this->userid)));
  | 
        
        
            | 
            | 
           442 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           443 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           444 | 
                   // No author.
  | 
        
        
            | 
            | 
           445 | 
                   $anotheruser = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           446 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'userid', 'value' => $anotheruser->id)));
  | 
        
        
            | 
            | 
           447 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           448 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           449 | 
               }
  | 
        
        
            | 
            | 
           450 | 
              | 
        
        
            | 
            | 
           451 | 
               /**
  | 
        
        
            | 
            | 
           452 | 
                * Test get entries filtering by entry.
  | 
        
        
            | 
            | 
           453 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           454 | 
               public function test_get_entries_filtering_by_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           455 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           456 | 
                   // Filter by correct entry.
  | 
        
        
            | 
            | 
           457 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'entryid', 'value' => $this->postid)));
  | 
        
        
            | 
            | 
           458 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           459 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           460 | 
                   // Non-existent entry.
  | 
        
        
            | 
            | 
           461 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           462 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'entryid', 'value' => -1)));
  | 
        
        
            | 
            | 
           463 | 
               }
  | 
        
        
            | 
            | 
           464 | 
              | 
        
        
            | 
            | 
           465 | 
               /**
  | 
        
        
            | 
            | 
           466 | 
                * Test get entries filtering by search.
  | 
        
        
            | 
            | 
           467 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           468 | 
               public function test_get_entries_filtering_by_search(): void {
  | 
        
        
           | 1 | 
           efrain | 
           469 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           470 | 
                   // Filter by correct search.
  | 
        
        
            | 
            | 
           471 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'search', 'value' => 'test')));
  | 
        
        
            | 
            | 
           472 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           473 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           474 | 
                   // Non-existent search.
  | 
        
        
            | 
            | 
           475 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'search', 'value' => 'abc')));
  | 
        
        
            | 
            | 
           476 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           477 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           478 | 
               }
  | 
        
        
            | 
            | 
           479 | 
              | 
        
        
            | 
            | 
           480 | 
               /**
  | 
        
        
            | 
            | 
           481 | 
                * Test get entries filtering by tag.
  | 
        
        
            | 
            | 
           482 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           483 | 
               public function test_get_entries_filtering_by_tag(): void {
  | 
        
        
           | 1 | 
           efrain | 
           484 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           485 | 
                   // Filter by correct tag.
  | 
        
        
            | 
            | 
           486 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'tag', 'value' => 'tag1')));
  | 
        
        
            | 
            | 
           487 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           488 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           489 | 
                   // Create tag.
  | 
        
        
            | 
            | 
           490 | 
                   $tag = $this->getDataGenerator()->create_tag(array('userid' => $this->userid, 'name' => 'tag2',
  | 
        
        
            | 
            | 
           491 | 
                       'isstandard' => 1));
  | 
        
        
            | 
            | 
           492 | 
              | 
        
        
            | 
            | 
           493 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'tag', 'value' => 'tag2')));
  | 
        
        
            | 
            | 
           494 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           495 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           496 | 
               }
  | 
        
        
            | 
            | 
           497 | 
              | 
        
        
            | 
            | 
           498 | 
               /**
  | 
        
        
            | 
            | 
           499 | 
                * Test get entries filtering by tag id.
  | 
        
        
            | 
            | 
           500 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           501 | 
               public function test_get_entries_filtering_by_tagid(): void {
  | 
        
        
           | 1 | 
           efrain | 
           502 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           503 | 
                   // Filter by correct tag.
  | 
        
        
            | 
            | 
           504 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'tagid', 'value' => $this->tagid)));
  | 
        
        
            | 
            | 
           505 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           506 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           507 | 
                   // Non-existent tag.
  | 
        
        
            | 
            | 
           508 | 
              | 
        
        
            | 
            | 
           509 | 
                   // Create tag.
  | 
        
        
            | 
            | 
           510 | 
                   $tag = $this->getDataGenerator()->create_tag(array('userid' => $this->userid, 'name' => 'tag2',
  | 
        
        
            | 
            | 
           511 | 
                       'isstandard' => 1));
  | 
        
        
            | 
            | 
           512 | 
              | 
        
        
            | 
            | 
           513 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'tagid', 'value' => $tag->id)));
  | 
        
        
            | 
            | 
           514 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           515 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           516 | 
               }
  | 
        
        
            | 
            | 
           517 | 
              | 
        
        
            | 
            | 
           518 | 
               /**
  | 
        
        
            | 
            | 
           519 | 
                * Test get entries filtering by group.
  | 
        
        
            | 
            | 
           520 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           521 | 
               public function test_get_entries_filtering_by_group(): void {
  | 
        
        
           | 1 | 
           efrain | 
           522 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           523 | 
                   // Add blog associations with a course.
  | 
        
        
            | 
            | 
           524 | 
                   $coursecontext = \context_course::instance($this->courseid);
  | 
        
        
            | 
            | 
           525 | 
                   $blog = new \blog_entry($this->postid);
  | 
        
        
            | 
            | 
           526 | 
                   $blog->add_association($coursecontext->id);
  | 
        
        
            | 
            | 
           527 | 
              | 
        
        
            | 
            | 
           528 | 
                   // Filter by correct group.
  | 
        
        
            | 
            | 
           529 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'groupid', 'value' => $this->groupid)));
  | 
        
        
            | 
            | 
           530 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           531 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           532 | 
                   // Non-existent group.
  | 
        
        
            | 
            | 
           533 | 
                   $anotheruser = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           534 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           535 | 
                   \core_blog\external::get_entries(array(array('name' => 'groupid', 'value' => -1)));
  | 
        
        
            | 
            | 
           536 | 
               }
  | 
        
        
            | 
            | 
           537 | 
              | 
        
        
            | 
            | 
           538 | 
               /**
  | 
        
        
            | 
            | 
           539 | 
                * Test get entries multiple filter.
  | 
        
        
            | 
            | 
           540 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           541 | 
               public function test_get_entries_multiple_filter(): void {
  | 
        
        
           | 1 | 
           efrain | 
           542 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           543 | 
                   // Add blog associations with a course.
  | 
        
        
            | 
            | 
           544 | 
                   $coursecontext = \context_course::instance($this->courseid);
  | 
        
        
            | 
            | 
           545 | 
                   $blog = new \blog_entry($this->postid);
  | 
        
        
            | 
            | 
           546 | 
                   $blog->add_association($coursecontext->id);
  | 
        
        
            | 
            | 
           547 | 
              | 
        
        
            | 
            | 
           548 | 
                   $result = \core_blog\external::get_entries(array(
  | 
        
        
            | 
            | 
           549 | 
                       array('name' => 'tagid', 'value' => $this->tagid),
  | 
        
        
            | 
            | 
           550 | 
                       array('name' => 'userid', 'value' => $this->userid),
  | 
        
        
            | 
            | 
           551 | 
                   ));
  | 
        
        
            | 
            | 
           552 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           553 | 
                   $this->assertCount(1, $result['entries']);
  | 
        
        
            | 
            | 
           554 | 
              | 
        
        
            | 
            | 
           555 | 
                   // Non-existent multiple filter.
  | 
        
        
            | 
            | 
           556 | 
                   $result = \core_blog\external::get_entries(array(
  | 
        
        
            | 
            | 
           557 | 
                       array('name' => 'search', 'value' => 'www'),
  | 
        
        
            | 
            | 
           558 | 
                       array('name' => 'userid', 'value' => $this->userid),
  | 
        
        
            | 
            | 
           559 | 
                   ));
  | 
        
        
            | 
            | 
           560 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           561 | 
                   $this->assertCount(0, $result['entries']);
  | 
        
        
            | 
            | 
           562 | 
               }
  | 
        
        
            | 
            | 
           563 | 
              | 
        
        
            | 
            | 
           564 | 
               /**
  | 
        
        
            | 
            | 
           565 | 
                * Test get entries filtering by invalid_filter.
  | 
        
        
            | 
            | 
           566 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           567 | 
               public function test_get_entries_filtering_by_invalid_filter(): void {
  | 
        
        
           | 1 | 
           efrain | 
           568 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           569 | 
                   // Filter by incorrect filter.
  | 
        
        
            | 
            | 
           570 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           571 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'zzZZzz', 'value' => 'wwWWww')));
  | 
        
        
            | 
            | 
           572 | 
               }
  | 
        
        
            | 
            | 
           573 | 
              | 
        
        
            | 
            | 
           574 | 
               /**
  | 
        
        
            | 
            | 
           575 | 
                * Test get entries when blog is disabled.
  | 
        
        
            | 
            | 
           576 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           577 | 
               public function test_get_entries_blog_disabled(): void {
  | 
        
        
           | 1 | 
           efrain | 
           578 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           579 | 
              | 
        
        
            | 
            | 
           580 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           581 | 
                   $CFG->enableblogs = 0;
  | 
        
        
            | 
            | 
           582 | 
                   // Filter by incorrect filter.
  | 
        
        
            | 
            | 
           583 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           584 | 
                   $result = \core_blog\external::get_entries(array(array('name' => 'zzZZzz', 'value' => 'wwWWww')));
  | 
        
        
            | 
            | 
           585 | 
               }
  | 
        
        
            | 
            | 
           586 | 
              | 
        
        
            | 
            | 
           587 | 
               /**
  | 
        
        
            | 
            | 
           588 | 
                * Test view_blog_entries without filter.
  | 
        
        
            | 
            | 
           589 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           590 | 
               public function test_view_blog_entries_without_filtering(): void {
  | 
        
        
           | 1 | 
           efrain | 
           591 | 
                   // Test user with full capabilities.
  | 
        
        
            | 
            | 
           592 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           593 | 
                   // Trigger and capture the event.
  | 
        
        
            | 
            | 
           594 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           595 | 
                   $result = \core_blog\external::view_entries();
  | 
        
        
            | 
            | 
           596 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::view_entries_returns(), $result);
  | 
        
        
            | 
            | 
           597 | 
              | 
        
        
            | 
            | 
           598 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           599 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           600 | 
                   $event = array_shift($events);
  | 
        
        
            | 
            | 
           601 | 
                   // Checking that the event contains the expected values (empty, no filtering done).
  | 
        
        
            | 
            | 
           602 | 
                   $this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
  | 
        
        
            | 
            | 
           603 | 
                   $this->assertEmpty($event->get_data()['relateduserid']);
  | 
        
        
            | 
            | 
           604 | 
                   $this->assertEmpty($event->get_data()['other']['entryid']);
  | 
        
        
            | 
            | 
           605 | 
                   $this->assertEmpty($event->get_data()['other']['tagid']);
  | 
        
        
            | 
            | 
           606 | 
                   $this->assertEmpty($event->get_data()['other']['userid']);
  | 
        
        
            | 
            | 
           607 | 
                   $this->assertEmpty($event->get_data()['other']['modid']);
  | 
        
        
            | 
            | 
           608 | 
                   $this->assertEmpty($event->get_data()['other']['groupid']);
  | 
        
        
            | 
            | 
           609 | 
                   $this->assertEmpty($event->get_data()['other']['search']);
  | 
        
        
            | 
            | 
           610 | 
                   $this->assertEmpty($event->get_data()['other']['courseid']);
  | 
        
        
            | 
            | 
           611 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           612 | 
                   $this->assertNotEmpty($event->get_name());
  | 
        
        
            | 
            | 
           613 | 
               }
  | 
        
        
            | 
            | 
           614 | 
              | 
        
        
            | 
            | 
           615 | 
               /**
  | 
        
        
            | 
            | 
           616 | 
                * Test view_blog_entries doing filtering.
  | 
        
        
            | 
            | 
           617 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           618 | 
               public function test_view_blog_entries_with_filtering(): void {
  | 
        
        
           | 1 | 
           efrain | 
           619 | 
                   // Test user with full capabilities.
  | 
        
        
            | 
            | 
           620 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           621 | 
                   // Trigger and capture the event.
  | 
        
        
            | 
            | 
           622 | 
                   $sink = $this->redirectEvents();
  | 
        
        
            | 
            | 
           623 | 
                   $result = \core_blog\external::view_entries(array(
  | 
        
        
            | 
            | 
           624 | 
                       array('name' => 'tagid', 'value' => $this->tagid),
  | 
        
        
            | 
            | 
           625 | 
                       array('name' => 'userid', 'value' => $this->userid),
  | 
        
        
            | 
            | 
           626 | 
                   ));
  | 
        
        
            | 
            | 
           627 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::view_entries_returns(), $result);
  | 
        
        
            | 
            | 
           628 | 
              | 
        
        
            | 
            | 
           629 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           630 | 
                   $this->assertCount(1, $events);
  | 
        
        
            | 
            | 
           631 | 
                   $event = array_shift($events);
  | 
        
        
            | 
            | 
           632 | 
                   // Checking that the event contains the expected values (filter by user and tag).
  | 
        
        
            | 
            | 
           633 | 
                   $this->assertInstanceOf('\core\event\blog_entries_viewed', $event);
  | 
        
        
            | 
            | 
           634 | 
                   $this->assertEquals($this->userid, $event->get_data()['relateduserid']);
  | 
        
        
            | 
            | 
           635 | 
                   $this->assertEmpty($event->get_data()['other']['entryid']);
  | 
        
        
            | 
            | 
           636 | 
                   $this->assertEquals($this->tagid, $event->get_data()['other']['tagid']);
  | 
        
        
            | 
            | 
           637 | 
                   $this->assertEquals($this->userid, $event->get_data()['other']['userid']);
  | 
        
        
            | 
            | 
           638 | 
                   $this->assertEmpty($event->get_data()['other']['modid']);
  | 
        
        
            | 
            | 
           639 | 
                   $this->assertEmpty($event->get_data()['other']['groupid']);
  | 
        
        
            | 
            | 
           640 | 
                   $this->assertEmpty($event->get_data()['other']['search']);
  | 
        
        
            | 
            | 
           641 | 
                   $this->assertEmpty($event->get_data()['other']['courseid']);
  | 
        
        
            | 
            | 
           642 | 
                   $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           643 | 
                   $this->assertNotEmpty($event->get_name());
  | 
        
        
            | 
            | 
           644 | 
               }
  | 
        
        
            | 
            | 
           645 | 
              | 
        
        
            | 
            | 
           646 | 
               /**
  | 
        
        
            | 
            | 
           647 | 
                * Test get_access_information
  | 
        
        
            | 
            | 
           648 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           649 | 
               public function test_get_access_information(): void {
  | 
        
        
           | 1 | 
           efrain | 
           650 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           651 | 
              | 
        
        
            | 
            | 
           652 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           653 | 
                   $result = get_access_information::execute();
  | 
        
        
            | 
            | 
           654 | 
                   $result = external_api::clean_returnvalue(get_access_information::execute_returns(), $result);
  | 
        
        
            | 
            | 
           655 | 
              | 
        
        
            | 
            | 
           656 | 
                   $this->assertTrue($result['canview']);
  | 
        
        
            | 
            | 
           657 | 
                   $this->assertTrue($result['cansearch']);
  | 
        
        
            | 
            | 
           658 | 
                   $this->assertTrue($result['canviewdrafts']);
  | 
        
        
            | 
            | 
           659 | 
                   $this->assertTrue($result['cancreate']);
  | 
        
        
            | 
            | 
           660 | 
                   $this->assertTrue($result['canmanageentries']);
  | 
        
        
            | 
            | 
           661 | 
                   $this->assertTrue($result['canmanageexternal']);
  | 
        
        
            | 
            | 
           662 | 
                   $this->assertEmpty($result['warnings']);
  | 
        
        
            | 
            | 
           663 | 
              | 
        
        
            | 
            | 
           664 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           665 | 
                   $result = get_access_information::execute();
  | 
        
        
            | 
            | 
           666 | 
                   $result = external_api::clean_returnvalue(get_access_information::execute_returns(), $result);
  | 
        
        
            | 
            | 
           667 | 
              | 
        
        
            | 
            | 
           668 | 
                   $this->assertTrue($result['canview']);
  | 
        
        
            | 
            | 
           669 | 
                   $this->assertTrue($result['cansearch']);
  | 
        
        
            | 
            | 
           670 | 
                   $this->assertFalse($result['canviewdrafts']);
  | 
        
        
            | 
            | 
           671 | 
                   $this->assertTrue($result['cancreate']);
  | 
        
        
            | 
            | 
           672 | 
                   $this->assertFalse($result['canmanageentries']);
  | 
        
        
            | 
            | 
           673 | 
                   $this->assertTrue($result['canmanageexternal']);
  | 
        
        
            | 
            | 
           674 | 
                   $this->assertEmpty($result['warnings']);
  | 
        
        
            | 
            | 
           675 | 
               }
  | 
        
        
            | 
            | 
           676 | 
              | 
        
        
            | 
            | 
           677 | 
               /**
  | 
        
        
            | 
            | 
           678 | 
                * Test add_entry
  | 
        
        
            | 
            | 
           679 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           680 | 
               public function test_add_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           681 | 
                   global $USER;
  | 
        
        
            | 
            | 
           682 | 
              | 
        
        
            | 
            | 
           683 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           684 | 
              | 
        
        
            | 
            | 
           685 | 
                   // Add post with attachments.
  | 
        
        
            | 
            | 
           686 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           687 | 
              | 
        
        
            | 
            | 
           688 | 
                   // Draft files.
  | 
        
        
            | 
            | 
           689 | 
                   $draftidinlineattach = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           690 | 
                   $draftidattach = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           691 | 
                   $usercontext = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           692 | 
                   $inlinefilename = 'inlineimage.png';
  | 
        
        
            | 
            | 
           693 | 
                   $filerecordinline = [
  | 
        
        
            | 
            | 
           694 | 
                       'contextid' => $usercontext->id,
  | 
        
        
            | 
            | 
           695 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           696 | 
                       'filearea'  => 'draft',
  | 
        
        
            | 
            | 
           697 | 
                       'itemid'    => $draftidinlineattach,
  | 
        
        
            | 
            | 
           698 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           699 | 
                       'filename'  => $inlinefilename,
  | 
        
        
            | 
            | 
           700 | 
                   ];
  | 
        
        
            | 
            | 
           701 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           702 | 
              | 
        
        
            | 
            | 
           703 | 
                   // Create a file in a draft area for regular attachments.
  | 
        
        
            | 
            | 
           704 | 
                   $filerecordattach = $filerecordinline;
  | 
        
        
            | 
            | 
           705 | 
                   $attachfilename = 'attachment.txt';
  | 
        
        
            | 
            | 
           706 | 
                   $filerecordattach['filename'] = $attachfilename;
  | 
        
        
            | 
            | 
           707 | 
                   $filerecordattach['itemid'] = $draftidattach;
  | 
        
        
            | 
            | 
           708 | 
                   $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
  | 
        
        
            | 
            | 
           709 | 
                   $fs->create_file_from_string($filerecordattach, 'simple text attachment');
  | 
        
        
            | 
            | 
           710 | 
              | 
        
        
            | 
            | 
           711 | 
                   $options = [
  | 
        
        
            | 
            | 
           712 | 
                       [
  | 
        
        
            | 
            | 
           713 | 
                           'name' => 'inlineattachmentsid',
  | 
        
        
            | 
            | 
           714 | 
                           'value' => $draftidinlineattach,
  | 
        
        
            | 
            | 
           715 | 
                       ],
  | 
        
        
            | 
            | 
           716 | 
                       [
  | 
        
        
            | 
            | 
           717 | 
                           'name' => 'attachmentsid',
  | 
        
        
            | 
            | 
           718 | 
                           'value' => $draftidattach,
  | 
        
        
            | 
            | 
           719 | 
                       ],
  | 
        
        
            | 
            | 
           720 | 
                       [
  | 
        
        
            | 
            | 
           721 | 
                           'name' => 'tags',
  | 
        
        
            | 
            | 
           722 | 
                           'value' => 'tag1, tag2',
  | 
        
        
            | 
            | 
           723 | 
                       ],
  | 
        
        
            | 
            | 
           724 | 
                       [
  | 
        
        
            | 
            | 
           725 | 
                           'name' => 'courseassoc',
  | 
        
        
            | 
            | 
           726 | 
                           'value' => $this->courseid,
  | 
        
        
            | 
            | 
           727 | 
                       ],
  | 
        
        
            | 
            | 
           728 | 
                   ];
  | 
        
        
            | 
            | 
           729 | 
              | 
        
        
            | 
            | 
           730 | 
                   $subject = 'First post';
  | 
        
        
            | 
            | 
           731 | 
                   $summary = 'First post summary';
  | 
        
        
            | 
            | 
           732 | 
                   $result = add_entry::execute($subject, $summary, FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           733 | 
                   $result = external_api::clean_returnvalue(add_entry::execute_returns(), $result);
  | 
        
        
            | 
            | 
           734 | 
                   $postid = $result['entryid'];
  | 
        
        
            | 
            | 
           735 | 
              | 
        
        
            | 
            | 
           736 | 
                   // Retrieve files via WS.
  | 
        
        
            | 
            | 
           737 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           738 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           739 | 
              | 
        
        
            | 
            | 
           740 | 
                   foreach ($result['entries'] as $entry) {
  | 
        
        
            | 
            | 
           741 | 
                       if ($entry['id'] == $postid) {
  | 
        
        
            | 
            | 
           742 | 
                           $this->assertEquals($subject, $entry['subject']);
  | 
        
        
            | 
            | 
           743 | 
                           $this->assertEquals($summary, $entry['summary']);
  | 
        
        
            | 
            | 
           744 | 
                           $this->assertEquals($this->courseid, $entry['courseid']);
  | 
        
        
            | 
            | 
           745 | 
                           $this->assertCount(1, $entry['attachmentfiles']);
  | 
        
        
            | 
            | 
           746 | 
                           $this->assertCount(1, $entry['summaryfiles']);
  | 
        
        
            | 
            | 
           747 | 
                           $this->assertCount(2, $entry['tags']);
  | 
        
        
            | 
            | 
           748 | 
                           $this->assertEquals($attachfilename, $entry['attachmentfiles'][0]['filename']);
  | 
        
        
            | 
            | 
           749 | 
                           $this->assertEquals($inlinefilename, $entry['summaryfiles'][0]['filename']);
  | 
        
        
            | 
            | 
           750 | 
                       }
  | 
        
        
            | 
            | 
           751 | 
                   }
  | 
        
        
            | 
            | 
           752 | 
               }
  | 
        
        
            | 
            | 
           753 | 
              | 
        
        
            | 
            | 
           754 | 
               /**
  | 
        
        
            | 
            | 
           755 | 
                * Test add_entry when blogs not enabled.
  | 
        
        
            | 
            | 
           756 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           757 | 
               public function test_add_entry_blog_not_enabled(): void {
  | 
        
        
           | 1 | 
           efrain | 
           758 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           759 | 
              | 
        
        
            | 
            | 
           760 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           761 | 
                   $CFG->enableblogs = 0;
  | 
        
        
            | 
            | 
           762 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           763 | 
              | 
        
        
            | 
            | 
           764 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           765 | 
                   $this->expectExceptionMessage(get_string('blogdisable', 'blog'));
  | 
        
        
            | 
            | 
           766 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML);
  | 
        
        
            | 
            | 
           767 | 
               }
  | 
        
        
            | 
            | 
           768 | 
              | 
        
        
            | 
            | 
           769 | 
               /**
  | 
        
        
            | 
            | 
           770 | 
                * Test add_entry without permissions.
  | 
        
        
            | 
            | 
           771 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           772 | 
               public function test_add_entry_no_permission(): void {
  | 
        
        
           | 1 | 
           efrain | 
           773 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           774 | 
              | 
        
        
            | 
            | 
           775 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           776 | 
              | 
        
        
            | 
            | 
           777 | 
                   // Remove capability.
  | 
        
        
            | 
            | 
           778 | 
                   $sitecontext = \context_system::instance();
  | 
        
        
            | 
            | 
           779 | 
                   $this->unassignUserCapability('moodle/blog:create', $sitecontext->id, $CFG->defaultuserroleid);
  | 
        
        
            | 
            | 
           780 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           781 | 
                   $this->setuser($user);
  | 
        
        
            | 
            | 
           782 | 
              | 
        
        
            | 
            | 
           783 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           784 | 
                   $this->expectExceptionMessage(get_string('cannoteditentryorblog', 'blog'));
  | 
        
        
            | 
            | 
           785 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML);
  | 
        
        
            | 
            | 
           786 | 
               }
  | 
        
        
            | 
            | 
           787 | 
              | 
        
        
            | 
            | 
           788 | 
               /**
  | 
        
        
            | 
            | 
           789 | 
                * Test add_entry invalid parameter.
  | 
        
        
            | 
            | 
           790 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           791 | 
               public function test_add_entry_invalid_parameter(): void {
  | 
        
        
           | 1 | 
           efrain | 
           792 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           793 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           794 | 
              | 
        
        
            | 
            | 
           795 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           796 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'invalid'));
  | 
        
        
            | 
            | 
           797 | 
                   $options = [['name' => 'invalid', 'value' => 'invalidvalue']];
  | 
        
        
            | 
            | 
           798 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           799 | 
               }
  | 
        
        
            | 
            | 
           800 | 
              | 
        
        
            | 
            | 
           801 | 
               /**
  | 
        
        
            | 
            | 
           802 | 
                * Test add_entry diabled associations.
  | 
        
        
            | 
            | 
           803 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           804 | 
               public function test_add_entry_disabled_assoc(): void {
  | 
        
        
           | 1 | 
           efrain | 
           805 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           806 | 
                   $CFG->useblogassociations = 0;
  | 
        
        
            | 
            | 
           807 | 
              | 
        
        
            | 
            | 
           808 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           809 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           810 | 
              | 
        
        
            | 
            | 
           811 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           812 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'modassoc'));
  | 
        
        
            | 
            | 
           813 | 
                   $options = [['name' => 'modassoc', 'value' => 1]];
  | 
        
        
            | 
            | 
           814 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           815 | 
               }
  | 
        
        
            | 
            | 
           816 | 
              | 
        
        
            | 
            | 
           817 | 
               /**
  | 
        
        
            | 
            | 
           818 | 
                * Test add_entry invalid publish state.
  | 
        
        
            | 
            | 
           819 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           820 | 
               public function test_add_entry_invalid_publishstate(): void {
  | 
        
        
           | 1 | 
           efrain | 
           821 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           822 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           823 | 
              | 
        
        
            | 
            | 
           824 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           825 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'publishstate'));
  | 
        
        
            | 
            | 
           826 | 
                   $options = [['name' => 'publishstate', 'value' => 'something']];
  | 
        
        
            | 
            | 
           827 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           828 | 
               }
  | 
        
        
            | 
            | 
           829 | 
              | 
        
        
            | 
            | 
           830 | 
               /**
  | 
        
        
            | 
            | 
           831 | 
                * Test add_entry invalid association.
  | 
        
        
            | 
            | 
           832 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           833 | 
               public function test_add_entry_invalid_association(): void {
  | 
        
        
           | 1 | 
           efrain | 
           834 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           835 | 
              | 
        
        
            | 
            | 
           836 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           837 | 
                   $anothercourse = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           838 | 
                   $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           839 | 
              | 
        
        
            | 
            | 
           840 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           841 | 
              | 
        
        
            | 
            | 
           842 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           843 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'modassoc'));
  | 
        
        
            | 
            | 
           844 | 
                   $options = [
  | 
        
        
            | 
            | 
           845 | 
                       ['name' => 'courseassoc', 'value' => $anothercourse->id],
  | 
        
        
            | 
            | 
           846 | 
                       ['name' => 'modassoc', 'value' => $page->cmid],
  | 
        
        
            | 
            | 
           847 | 
                   ];
  | 
        
        
            | 
            | 
           848 | 
                   add_entry::execute('Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           849 | 
               }
  | 
        
        
            | 
            | 
           850 | 
              | 
        
        
            | 
            | 
           851 | 
               /**
  | 
        
        
            | 
            | 
           852 | 
                * Test delete_entry
  | 
        
        
            | 
            | 
           853 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           854 | 
               public function test_delete_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           855 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           856 | 
              | 
        
        
            | 
            | 
           857 | 
                   // I can delete my own entry.
  | 
        
        
            | 
            | 
           858 | 
                   $this->setUser($this->userid);
  | 
        
        
            | 
            | 
           859 | 
              | 
        
        
            | 
            | 
           860 | 
                   $result = delete_entry::execute($this->postid);
  | 
        
        
            | 
            | 
           861 | 
                   $result = external_api::clean_returnvalue(delete_entry::execute_returns(), $result);
  | 
        
        
            | 
            | 
           862 | 
                   $this->assertTrue($result['status']);
  | 
        
        
            | 
            | 
           863 | 
               }
  | 
        
        
            | 
            | 
           864 | 
              | 
        
        
            | 
            | 
           865 | 
               /**
  | 
        
        
            | 
            | 
           866 | 
                * Test delete_entry from another user (no permissions)
  | 
        
        
            | 
            | 
           867 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           868 | 
               public function test_delete_entry_no_permissions(): void {
  | 
        
        
           | 1 | 
           efrain | 
           869 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           870 | 
              | 
        
        
            | 
            | 
           871 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           872 | 
                   $this->getDataGenerator()->enrol_user($user->id, $this->courseid);
  | 
        
        
            | 
            | 
           873 | 
              | 
        
        
            | 
            | 
           874 | 
                   // I can delete my own entry.
  | 
        
        
            | 
            | 
           875 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           876 | 
              | 
        
        
            | 
            | 
           877 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           878 | 
                   delete_entry::execute($this->postid);
  | 
        
        
            | 
            | 
           879 | 
               }
  | 
        
        
            | 
            | 
           880 | 
              | 
        
        
            | 
            | 
           881 | 
               /**
  | 
        
        
            | 
            | 
           882 | 
                * Test delete_entry when blogs not enabled.
  | 
        
        
            | 
            | 
           883 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           884 | 
               public function test_delete_entry_blog_not_enabled(): void {
  | 
        
        
           | 1 | 
           efrain | 
           885 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           886 | 
              | 
        
        
            | 
            | 
           887 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           888 | 
                   $CFG->enableblogs = 0;
  | 
        
        
            | 
            | 
           889 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           890 | 
              | 
        
        
            | 
            | 
           891 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           892 | 
                   $this->expectExceptionMessage(get_string('blogdisable', 'blog'));
  | 
        
        
            | 
            | 
           893 | 
                   delete_entry::execute($this->postid);
  | 
        
        
            | 
            | 
           894 | 
               }
  | 
        
        
            | 
            | 
           895 | 
              | 
        
        
            | 
            | 
           896 | 
               /**
  | 
        
        
            | 
            | 
           897 | 
                * Test delete_entry invalid entry id.
  | 
        
        
            | 
            | 
           898 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           899 | 
               public function test_delete_entry_invalid_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           900 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           901 | 
              | 
        
        
            | 
            | 
           902 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           903 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           904 | 
              | 
        
        
            | 
            | 
           905 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           906 | 
                   delete_entry::execute($this->postid + 1000);
  | 
        
        
            | 
            | 
           907 | 
               }
  | 
        
        
            | 
            | 
           908 | 
              | 
        
        
            | 
            | 
           909 | 
               /**
  | 
        
        
            | 
            | 
           910 | 
                * Test prepare_entry_for_edition.
  | 
        
        
            | 
            | 
           911 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           912 | 
               public function test_prepare_entry_for_edition(): void {
  | 
        
        
           | 1 | 
           efrain | 
           913 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           914 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           915 | 
              | 
        
        
            | 
            | 
           916 | 
                   $result = prepare_entry_for_edition::execute($this->postid);
  | 
        
        
            | 
            | 
           917 | 
                   $result = external_api::clean_returnvalue(prepare_entry_for_edition::execute_returns(), $result);
  | 
        
        
            | 
            | 
           918 | 
                   $this->assertCount(2, $result['areas']);
  | 
        
        
            | 
            | 
           919 | 
                   $this->assertIsInt($result['inlineattachmentsid']);
  | 
        
        
            | 
            | 
           920 | 
                   $this->assertIsInt($result['attachmentsid']);
  | 
        
        
            | 
            | 
           921 | 
                   foreach ($result['areas'] as $area) {
  | 
        
        
            | 
            | 
           922 | 
                       if ($area['area'] == 'summary') {
  | 
        
        
            | 
            | 
           923 | 
                           $this->assertCount(4, $area['options']);
  | 
        
        
            | 
            | 
           924 | 
                       } else {
  | 
        
        
            | 
            | 
           925 | 
                           $this->assertEquals('attachment', $area['area']);
  | 
        
        
            | 
            | 
           926 | 
                           $this->assertCount(3, $area['options']);
  | 
        
        
            | 
            | 
           927 | 
                       }
  | 
        
        
            | 
            | 
           928 | 
                   }
  | 
        
        
            | 
            | 
           929 | 
               }
  | 
        
        
            | 
            | 
           930 | 
              | 
        
        
            | 
            | 
           931 | 
               /**
  | 
        
        
            | 
            | 
           932 | 
                * Test prepare_entry_for_edition when blogs not enabled.
  | 
        
        
            | 
            | 
           933 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           934 | 
               public function test_prepare_entry_for_edition_blog_not_enabled(): void {
  | 
        
        
           | 1 | 
           efrain | 
           935 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           936 | 
              | 
        
        
            | 
            | 
           937 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           938 | 
                   $CFG->enableblogs = 0;
  | 
        
        
            | 
            | 
           939 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           940 | 
              | 
        
        
            | 
            | 
           941 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           942 | 
                   $this->expectExceptionMessage(get_string('blogdisable', 'blog'));
  | 
        
        
            | 
            | 
           943 | 
                   prepare_entry_for_edition::execute($this->postid);
  | 
        
        
            | 
            | 
           944 | 
               }
  | 
        
        
            | 
            | 
           945 | 
              | 
        
        
            | 
            | 
           946 | 
               /**
  | 
        
        
            | 
            | 
           947 | 
                * Test prepare_entry_for_edition invalid entry id.
  | 
        
        
            | 
            | 
           948 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           949 | 
               public function test_prepare_entry_for_edition_invalid_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           950 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           951 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           952 | 
              | 
        
        
            | 
            | 
           953 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           954 | 
                   prepare_entry_for_edition::execute($this->postid + 1000);
  | 
        
        
            | 
            | 
           955 | 
               }
  | 
        
        
            | 
            | 
           956 | 
              | 
        
        
            | 
            | 
           957 | 
               /**
  | 
        
        
            | 
            | 
           958 | 
                * Test prepare_entry_for_edition without permissions.
  | 
        
        
            | 
            | 
           959 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           960 | 
               public function test_prepare_entry_for_edition_no_permission(): void {
  | 
        
        
           | 1 | 
           efrain | 
           961 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           962 | 
              | 
        
        
            | 
            | 
           963 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           964 | 
                   $this->getDataGenerator()->enrol_user($user->id, $this->courseid);
  | 
        
        
            | 
            | 
           965 | 
                   $this->setuser($user);
  | 
        
        
            | 
            | 
           966 | 
              | 
        
        
            | 
            | 
           967 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           968 | 
                   $this->expectExceptionMessage(get_string('cannoteditentryorblog', 'blog'));
  | 
        
        
            | 
            | 
           969 | 
                   prepare_entry_for_edition::execute($this->postid);
  | 
        
        
            | 
            | 
           970 | 
               }
  | 
        
        
            | 
            | 
           971 | 
              | 
        
        
            | 
            | 
           972 | 
               /**
  | 
        
        
            | 
            | 
           973 | 
                * Test update_entry
  | 
        
        
            | 
            | 
           974 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           975 | 
               public function test_update_entry(): void {
  | 
        
        
           | 1 | 
           efrain | 
           976 | 
                   global $USER;
  | 
        
        
            | 
            | 
           977 | 
              | 
        
        
            | 
            | 
           978 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           979 | 
              | 
        
        
            | 
            | 
           980 | 
                   // Add post with attachments.
  | 
        
        
            | 
            | 
           981 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           982 | 
              | 
        
        
            | 
            | 
           983 | 
                   // Draft files.
  | 
        
        
            | 
            | 
           984 | 
                   $draftidinlineattach = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           985 | 
                   $draftidattach = file_get_unused_draft_itemid();
  | 
        
        
            | 
            | 
           986 | 
                   $usercontext = \context_user::instance($USER->id);
  | 
        
        
            | 
            | 
           987 | 
                   $inlinefilename = 'inlineimage.png';
  | 
        
        
            | 
            | 
           988 | 
                   $filerecordinline = [
  | 
        
        
            | 
            | 
           989 | 
                       'contextid' => $usercontext->id,
  | 
        
        
            | 
            | 
           990 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           991 | 
                       'filearea'  => 'draft',
  | 
        
        
            | 
            | 
           992 | 
                       'itemid'    => $draftidinlineattach,
  | 
        
        
            | 
            | 
           993 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           994 | 
                       'filename'  => $inlinefilename,
  | 
        
        
            | 
            | 
           995 | 
                   ];
  | 
        
        
            | 
            | 
           996 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           997 | 
              | 
        
        
            | 
            | 
           998 | 
                   // Create a file in a draft area for regular attachments.
  | 
        
        
            | 
            | 
           999 | 
                   $filerecordattach = $filerecordinline;
  | 
        
        
            | 
            | 
           1000 | 
                   $attachfilename = 'attachment.txt';
  | 
        
        
            | 
            | 
           1001 | 
                   $filerecordattach['filename'] = $attachfilename;
  | 
        
        
            | 
            | 
           1002 | 
                   $filerecordattach['itemid'] = $draftidattach;
  | 
        
        
            | 
            | 
           1003 | 
                   $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
  | 
        
        
            | 
            | 
           1004 | 
                   $fs->create_file_from_string($filerecordattach, 'simple text attachment');
  | 
        
        
            | 
            | 
           1005 | 
              | 
        
        
            | 
            | 
           1006 | 
                   $options = [
  | 
        
        
            | 
            | 
           1007 | 
                       [
  | 
        
        
            | 
            | 
           1008 | 
                           'name' => 'inlineattachmentsid',
  | 
        
        
            | 
            | 
           1009 | 
                           'value' => $draftidinlineattach,
  | 
        
        
            | 
            | 
           1010 | 
                       ],
  | 
        
        
            | 
            | 
           1011 | 
                       [
  | 
        
        
            | 
            | 
           1012 | 
                           'name' => 'attachmentsid',
  | 
        
        
            | 
            | 
           1013 | 
                           'value' => $draftidattach,
  | 
        
        
            | 
            | 
           1014 | 
                       ],
  | 
        
        
            | 
            | 
           1015 | 
                       [
  | 
        
        
            | 
            | 
           1016 | 
                           'name' => 'tags',
  | 
        
        
            | 
            | 
           1017 | 
                           'value' => 'tag1, tag2',
  | 
        
        
            | 
            | 
           1018 | 
                       ],
  | 
        
        
            | 
            | 
           1019 | 
                       [
  | 
        
        
            | 
            | 
           1020 | 
                           'name' => 'courseassoc',
  | 
        
        
            | 
            | 
           1021 | 
                           'value' => $this->courseid,
  | 
        
        
            | 
            | 
           1022 | 
                       ],
  | 
        
        
            | 
            | 
           1023 | 
                   ];
  | 
        
        
            | 
            | 
           1024 | 
              | 
        
        
            | 
            | 
           1025 | 
                   $subject = 'First post';
  | 
        
        
            | 
            | 
           1026 | 
                   $summary = 'First post summary';
  | 
        
        
            | 
            | 
           1027 | 
                   $result = add_entry::execute($subject, $summary, FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1028 | 
                   $result = external_api::clean_returnvalue(add_entry::execute_returns(), $result);
  | 
        
        
            | 
            | 
           1029 | 
                   $entryid = $result['entryid'];
  | 
        
        
            | 
            | 
           1030 | 
              | 
        
        
            | 
            | 
           1031 | 
                   // Retrieve file areas.
  | 
        
        
            | 
            | 
           1032 | 
                   $result = prepare_entry_for_edition::execute($entryid);
  | 
        
        
            | 
            | 
           1033 | 
                   $result = external_api::clean_returnvalue(prepare_entry_for_edition::execute_returns(), $result);
  | 
        
        
            | 
            | 
           1034 | 
              | 
        
        
            | 
            | 
           1035 | 
                   // Update files.
  | 
        
        
            | 
            | 
           1036 | 
                   $inlinefilename = 'inlineimage2.png';
  | 
        
        
            | 
            | 
           1037 | 
                   $filerecordinline = [
  | 
        
        
            | 
            | 
           1038 | 
                       'contextid' => $usercontext->id,
  | 
        
        
            | 
            | 
           1039 | 
                       'component' => 'user',
  | 
        
        
            | 
            | 
           1040 | 
                       'filearea'  => 'draft',
  | 
        
        
            | 
            | 
           1041 | 
                       'itemid'    => $result['inlineattachmentsid'],
  | 
        
        
            | 
            | 
           1042 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           1043 | 
                       'filename'  => $inlinefilename,
  | 
        
        
            | 
            | 
           1044 | 
                   ];
  | 
        
        
            | 
            | 
           1045 | 
                   $fs = get_file_storage();
  | 
        
        
            | 
            | 
           1046 | 
              | 
        
        
            | 
            | 
           1047 | 
                   // Create a file in a draft area for regular attachments.
  | 
        
        
            | 
            | 
           1048 | 
                   $filerecordattach = $filerecordinline;
  | 
        
        
            | 
            | 
           1049 | 
                   $newattachfilename = 'attachment2.txt';
  | 
        
        
            | 
            | 
           1050 | 
                   $filerecordattach['filename'] = $newattachfilename;
  | 
        
        
            | 
            | 
           1051 | 
                   $filerecordattach['itemid'] = $result['attachmentsid'];
  | 
        
        
            | 
            | 
           1052 | 
                   $fs->create_file_from_string($filerecordinline, 'image contents (not really)');
  | 
        
        
            | 
            | 
           1053 | 
                   $fs->create_file_from_string($filerecordattach, 'simple text attachment');
  | 
        
        
            | 
            | 
           1054 | 
              | 
        
        
            | 
            | 
           1055 | 
                   // Remove one previous attachment file.
  | 
        
        
            | 
            | 
           1056 | 
                   $filetoremove = (object) ['filename' => 'attachment.txt', 'filepath' => '/'];
  | 
        
        
            | 
            | 
           1057 | 
                   repository_delete_selected_files($usercontext, 'user', 'draft', $result['attachmentsid'], [$filetoremove]);
  | 
        
        
            | 
            | 
           1058 | 
              | 
        
        
            | 
            | 
           1059 | 
                   // Update.
  | 
        
        
            | 
            | 
           1060 | 
                   $options = [
  | 
        
        
            | 
            | 
           1061 | 
                       ['name' => 'inlineattachmentsid', 'value' => $result['inlineattachmentsid']],
  | 
        
        
            | 
            | 
           1062 | 
                       ['name' => 'attachmentsid', 'value' => $result['attachmentsid']],
  | 
        
        
            | 
            | 
           1063 | 
                       ['name' => 'tags', 'value' => 'tag3'],
  | 
        
        
            | 
            | 
           1064 | 
                       ['name' => 'courseassoc', 'value' => $this->courseid],
  | 
        
        
            | 
            | 
           1065 | 
                       ['name' => 'modassoc', 'value' => $this->cmid],
  | 
        
        
            | 
            | 
           1066 | 
                   ];
  | 
        
        
            | 
            | 
           1067 | 
              | 
        
        
            | 
            | 
           1068 | 
                   $subject = 'First post updated';
  | 
        
        
            | 
            | 
           1069 | 
                   $summary = 'First post summary updated';
  | 
        
        
            | 
            | 
           1070 | 
                   $result = update_entry::execute($entryid, $subject, $summary, FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1071 | 
                   $result = external_api::clean_returnvalue(update_entry::execute_returns(), $result);
  | 
        
        
            | 
            | 
           1072 | 
              | 
        
        
            | 
            | 
           1073 | 
                   // Retrieve files via WS.
  | 
        
        
            | 
            | 
           1074 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           1075 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           1076 | 
              | 
        
        
            | 
            | 
           1077 | 
                   foreach ($result['entries'] as $entry) {
  | 
        
        
            | 
            | 
           1078 | 
                       if ($entry['id'] == $entryid) {
  | 
        
        
            | 
            | 
           1079 | 
                           $this->assertEquals($subject, $entry['subject']);
  | 
        
        
            | 
            | 
           1080 | 
                           $this->assertEquals($summary, $entry['summary']);
  | 
        
        
            | 
            | 
           1081 | 
                           $this->assertEquals($this->courseid, $entry['courseid']);
  | 
        
        
            | 
            | 
           1082 | 
                           $this->assertEquals($this->cmid, $entry['coursemoduleid']);
  | 
        
        
            | 
            | 
           1083 | 
                           $this->assertCount(1, $entry['attachmentfiles']);
  | 
        
        
            | 
            | 
           1084 | 
                           $this->assertCount(2, $entry['summaryfiles']);
  | 
        
        
            | 
            | 
           1085 | 
                           $this->assertCount(1, $entry['tags']);
  | 
        
        
            | 
            | 
           1086 | 
                           $this->assertEquals($newattachfilename, $entry['attachmentfiles'][0]['filename']);
  | 
        
        
            | 
            | 
           1087 | 
                       }
  | 
        
        
            | 
            | 
           1088 | 
                   }
  | 
        
        
            | 
            | 
           1089 | 
              | 
        
        
            | 
            | 
           1090 | 
                   // Update removing associations.
  | 
        
        
            | 
            | 
           1091 | 
                   $options = [
  | 
        
        
            | 
            | 
           1092 | 
                       ['name' => 'courseassoc', 'value' => 0],
  | 
        
        
            | 
            | 
           1093 | 
                       ['name' => 'modassoc', 'value' => 0],
  | 
        
        
            | 
            | 
           1094 | 
                   ];
  | 
        
        
            | 
            | 
           1095 | 
              | 
        
        
            | 
            | 
           1096 | 
                   $result = update_entry::execute($entryid, $subject, $summary, FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1097 | 
                   $result = external_api::clean_returnvalue(update_entry::execute_returns(), $result);
  | 
        
        
            | 
            | 
           1098 | 
              | 
        
        
            | 
            | 
           1099 | 
                   // Retrieve files via WS.
  | 
        
        
            | 
            | 
           1100 | 
                   $result = \core_blog\external::get_entries();
  | 
        
        
            | 
            | 
           1101 | 
                   $result = external_api::clean_returnvalue(\core_blog\external::get_entries_returns(), $result);
  | 
        
        
            | 
            | 
           1102 | 
              | 
        
        
            | 
            | 
           1103 | 
                   foreach ($result['entries'] as $entry) {
  | 
        
        
            | 
            | 
           1104 | 
                       if ($entry['id'] == $entryid) {
  | 
        
        
            | 
            | 
           1105 | 
                           $this->assertEmpty($entry['courseid']);
  | 
        
        
            | 
            | 
           1106 | 
                           $this->assertEmpty($entry['coursemoduleid']);
  | 
        
        
            | 
            | 
           1107 | 
                       }
  | 
        
        
            | 
            | 
           1108 | 
                   }
  | 
        
        
            | 
            | 
           1109 | 
               }
  | 
        
        
            | 
            | 
           1110 | 
              | 
        
        
            | 
            | 
           1111 | 
               /**
  | 
        
        
            | 
            | 
           1112 | 
                * Test update_entry when blogs not enabled.
  | 
        
        
            | 
            | 
           1113 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1114 | 
               public function test_update_entry_blog_not_enabled(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1115 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           1116 | 
              | 
        
        
            | 
            | 
           1117 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1118 | 
                   $CFG->enableblogs = 0;
  | 
        
        
            | 
            | 
           1119 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           1120 | 
              | 
        
        
            | 
            | 
           1121 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1122 | 
                   $this->expectExceptionMessage(get_string('blogdisable', 'blog'));
  | 
        
        
            | 
            | 
           1123 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML);
  | 
        
        
            | 
            | 
           1124 | 
               }
  | 
        
        
            | 
            | 
           1125 | 
              | 
        
        
            | 
            | 
           1126 | 
               /**
  | 
        
        
            | 
            | 
           1127 | 
                * Test update_entry without permissions.
  | 
        
        
            | 
            | 
           1128 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1129 | 
               public function test_update_entry_no_permission(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1130 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           1131 | 
              | 
        
        
            | 
            | 
           1132 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1133 | 
              | 
        
        
            | 
            | 
           1134 | 
                   // Remove capability.
  | 
        
        
            | 
            | 
           1135 | 
                   $sitecontext = \context_system::instance();
  | 
        
        
            | 
            | 
           1136 | 
                   $this->unassignUserCapability('moodle/blog:create', $sitecontext->id, $CFG->defaultuserroleid);
  | 
        
        
            | 
            | 
           1137 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           1138 | 
                   $this->setuser($user);
  | 
        
        
            | 
            | 
           1139 | 
              | 
        
        
            | 
            | 
           1140 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1141 | 
                   $this->expectExceptionMessage(get_string('cannoteditentryorblog', 'blog'));
  | 
        
        
            | 
            | 
           1142 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML);
  | 
        
        
            | 
            | 
           1143 | 
               }
  | 
        
        
            | 
            | 
           1144 | 
              | 
        
        
            | 
            | 
           1145 | 
               /**
  | 
        
        
            | 
            | 
           1146 | 
                * Test update_entry invalid parameter.
  | 
        
        
            | 
            | 
           1147 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1148 | 
               public function test_update_entry_invalid_parameter(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1149 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1150 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           1151 | 
              | 
        
        
            | 
            | 
           1152 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1153 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'invalid'));
  | 
        
        
            | 
            | 
           1154 | 
                   $options = [['name' => 'invalid', 'value' => 'invalidvalue']];
  | 
        
        
            | 
            | 
           1155 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1156 | 
               }
  | 
        
        
            | 
            | 
           1157 | 
              | 
        
        
            | 
            | 
           1158 | 
               /**
  | 
        
        
            | 
            | 
           1159 | 
                * Test update_entry diabled associations.
  | 
        
        
            | 
            | 
           1160 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1161 | 
               public function test_update_entry_disabled_assoc(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1162 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           1163 | 
                   $CFG->useblogassociations = 0;
  | 
        
        
            | 
            | 
           1164 | 
              | 
        
        
            | 
            | 
           1165 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1166 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           1167 | 
              | 
        
        
            | 
            | 
           1168 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1169 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'modassoc'));
  | 
        
        
            | 
            | 
           1170 | 
                   $options = [['name' => 'modassoc', 'value' => 1]];
  | 
        
        
            | 
            | 
           1171 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1172 | 
               }
  | 
        
        
            | 
            | 
           1173 | 
              | 
        
        
            | 
            | 
           1174 | 
               /**
  | 
        
        
            | 
            | 
           1175 | 
                * Test update_entry invalid publish state.
  | 
        
        
            | 
            | 
           1176 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1177 | 
               public function test_update_entry_invalid_publishstate(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1178 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1179 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           1180 | 
              | 
        
        
            | 
            | 
           1181 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1182 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'publishstate'));
  | 
        
        
            | 
            | 
           1183 | 
                   $options = [['name' => 'publishstate', 'value' => 'something']];
  | 
        
        
            | 
            | 
           1184 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1185 | 
               }
  | 
        
        
            | 
            | 
           1186 | 
              | 
        
        
            | 
            | 
           1187 | 
               /**
  | 
        
        
            | 
            | 
           1188 | 
                * Test update_entry invalid association.
  | 
        
        
            | 
            | 
           1189 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1190 | 
               public function test_update_entry_invalid_association(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1191 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1192 | 
              | 
        
        
            | 
            | 
           1193 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           1194 | 
                   $anothercourse = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           1195 | 
                   $page = $this->getDataGenerator()->create_module('page', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           1196 | 
              | 
        
        
            | 
            | 
           1197 | 
                   $this->setAdminUser();
  | 
        
        
            | 
            | 
           1198 | 
              | 
        
        
            | 
            | 
           1199 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1200 | 
                   $this->expectExceptionMessage(get_string('errorinvalidparam', 'webservice', 'modassoc'));
  | 
        
        
            | 
            | 
           1201 | 
                   $options = [
  | 
        
        
            | 
            | 
           1202 | 
                       ['name' => 'courseassoc', 'value' => $anothercourse->id],
  | 
        
        
            | 
            | 
           1203 | 
                       ['name' => 'modassoc', 'value' => $page->cmid],
  | 
        
        
            | 
            | 
           1204 | 
                   ];
  | 
        
        
            | 
            | 
           1205 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML, $options);
  | 
        
        
            | 
            | 
           1206 | 
               }
  | 
        
        
            | 
            | 
           1207 | 
              | 
        
        
            | 
            | 
           1208 | 
               /**
  | 
        
        
            | 
            | 
           1209 | 
                * Test update_entry from another user (no permissions)
  | 
        
        
            | 
            | 
           1210 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           1211 | 
               public function test_update_entry_no_permissions(): void {
  | 
        
        
           | 1 | 
           efrain | 
           1212 | 
                   $this->resetAfterTest(true);
  | 
        
        
            | 
            | 
           1213 | 
              | 
        
        
            | 
            | 
           1214 | 
                   $user = $this->getDataGenerator()->create_user();
  | 
        
        
            | 
            | 
           1215 | 
                   $this->getDataGenerator()->enrol_user($user->id, $this->courseid);
  | 
        
        
            | 
            | 
           1216 | 
              | 
        
        
            | 
            | 
           1217 | 
                   // I can delete my own entry.
  | 
        
        
            | 
            | 
           1218 | 
                   $this->setUser($user);
  | 
        
        
            | 
            | 
           1219 | 
              | 
        
        
            | 
            | 
           1220 | 
                   $this->expectException('\moodle_exception');
  | 
        
        
            | 
            | 
           1221 | 
                   update_entry::execute($this->postid, 'Subject', 'Summary', FORMAT_HTML);
  | 
        
        
            | 
            | 
           1222 | 
               }
  | 
        
        
            | 
            | 
           1223 | 
           }
  |