| 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 |  * External function test for delete_content.
 | 
        
           |  |  | 19 |  *
 | 
        
           |  |  | 20 |  * @package    core_contentbank
 | 
        
           |  |  | 21 |  * @category   external
 | 
        
           |  |  | 22 |  * @since      Moodle 3.9
 | 
        
           |  |  | 23 |  * @copyright  2020 Sara Arjona <sara@moodle.com>
 | 
        
           |  |  | 24 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 25 |  */
 | 
        
           |  |  | 26 |   | 
        
           |  |  | 27 | namespace core_contentbank\external;
 | 
        
           |  |  | 28 |   | 
        
           |  |  | 29 | defined('MOODLE_INTERNAL') || die();
 | 
        
           |  |  | 30 |   | 
        
           |  |  | 31 | global $CFG;
 | 
        
           |  |  | 32 | require_once($CFG->dirroot . '/webservice/tests/helpers.php');
 | 
        
           |  |  | 33 | require_once($CFG->dirroot . '/contentbank/tests/fixtures/testable_content.php');
 | 
        
           |  |  | 34 |   | 
        
           |  |  | 35 | use dml_missing_record_exception;
 | 
        
           |  |  | 36 | use core_external\external_api;
 | 
        
           |  |  | 37 | use externallib_advanced_testcase;
 | 
        
           |  |  | 38 |   | 
        
           |  |  | 39 | /**
 | 
        
           |  |  | 40 |  * External function test for delete_content.
 | 
        
           |  |  | 41 |  *
 | 
        
           |  |  | 42 |  * @package    core_contentbank
 | 
        
           |  |  | 43 |  * @copyright  2020 Sara Arjona <sara@moodle.com>
 | 
        
           |  |  | 44 |  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
 | 
        
           |  |  | 45 |  */
 | 
        
           |  |  | 46 | class delete_content_test extends externallib_advanced_testcase {
 | 
        
           |  |  | 47 |   | 
        
           |  |  | 48 |     /**
 | 
        
           |  |  | 49 |      * Test the behaviour of delete_content().
 | 
        
           |  |  | 50 |      */
 | 
        
           |  |  | 51 |     public function test_delete_content() {
 | 
        
           |  |  | 52 |         global $DB;
 | 
        
           |  |  | 53 |         $this->resetAfterTest();
 | 
        
           |  |  | 54 |         $records = [];
 | 
        
           |  |  | 55 |   | 
        
           |  |  | 56 |         // Create users.
 | 
        
           |  |  | 57 |         $user = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 58 |         $roleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
 | 
        
           |  |  | 59 |         $manager = $this->getDataGenerator()->create_user();
 | 
        
           |  |  | 60 |         $this->getDataGenerator()->role_assign($roleid, $manager->id);
 | 
        
           |  |  | 61 |   | 
        
           |  |  | 62 |         // Add some content to the content bank.
 | 
        
           |  |  | 63 |         $generator = $this->getDataGenerator()->get_plugin_generator('core_contentbank');
 | 
        
           |  |  | 64 |         $records[$manager->id] = $generator->generate_contentbank_data('contenttype_testable', 4, $manager->id, null, false);
 | 
        
           |  |  | 65 |         $records[$user->id] = $generator->generate_contentbank_data('contenttype_testable', 2, $user->id, null, false);
 | 
        
           |  |  | 66 |   | 
        
           |  |  | 67 |         // Check the content has been created as expected.
 | 
        
           |  |  | 68 |         $this->assertEquals(6, $DB->count_records('contentbank_content'));
 | 
        
           |  |  | 69 |   | 
        
           |  |  | 70 |         // Check the content is deleted as expected by the user when the content has been created by herself.
 | 
        
           |  |  | 71 |         $this->setUser($user);
 | 
        
           |  |  | 72 |         $userrecord = array_shift($records[$user->id]);
 | 
        
           |  |  | 73 |         $result = delete_content::execute([$userrecord->id]);
 | 
        
           |  |  | 74 |         $result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
 | 
        
           |  |  | 75 |         $this->assertTrue($result['result']);
 | 
        
           |  |  | 76 |         $this->assertCount(0, $result['warnings']);
 | 
        
           |  |  | 77 |         $this->assertEquals(5, $DB->count_records('contentbank_content'));
 | 
        
           |  |  | 78 |   | 
        
           |  |  | 79 |         // Check the content is not deleted if the user hasn't created it and has only permission to delete her own content.
 | 
        
           |  |  | 80 |         $userrecord = array_shift($records[$user->id]);
 | 
        
           |  |  | 81 |         $managerrecord1 = array_shift($records[$manager->id]);
 | 
        
           |  |  | 82 |         $result = delete_content::execute([$managerrecord1->id, $userrecord->id]);
 | 
        
           |  |  | 83 |         $result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
 | 
        
           |  |  | 84 |         $this->assertFalse($result['result']);
 | 
        
           |  |  | 85 |         $this->assertCount(1, $result['warnings']);
 | 
        
           |  |  | 86 |         $warning = array_shift($result['warnings']);
 | 
        
           |  |  | 87 |         $this->assertEquals('nopermissiontodelete', $warning['warningcode']);
 | 
        
           |  |  | 88 |         $this->assertEquals($managerrecord1->id, $warning['item']);
 | 
        
           |  |  | 89 |         $this->assertEquals(4, $DB->count_records('contentbank_content'));
 | 
        
           |  |  | 90 |   | 
        
           |  |  | 91 |         // Check the content is deleted as expected by the manager.
 | 
        
           |  |  | 92 |         $this->setUser($manager);
 | 
        
           |  |  | 93 |         $managerrecord2 = array_shift($records[$manager->id]);
 | 
        
           |  |  | 94 |         $result = delete_content::execute([$managerrecord1->id, $managerrecord2->id]);
 | 
        
           |  |  | 95 |         $result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
 | 
        
           |  |  | 96 |         $this->assertTrue($result['result']);
 | 
        
           |  |  | 97 |         $this->assertCount(0, $result['warnings']);
 | 
        
           |  |  | 98 |         $this->assertEquals(2, $DB->count_records('contentbank_content'));
 | 
        
           |  |  | 99 |   | 
        
           |  |  | 100 |         // Check an exception warning is returned if an unexisting contentid is deleted.
 | 
        
           |  |  | 101 |         // Check also the other content is deleted (so the process continues after the exception is thrown).
 | 
        
           |  |  | 102 |         $managerrecord3 = array_shift($records[$manager->id]);
 | 
        
           |  |  | 103 |         $result = delete_content::execute([$managerrecord1->id, $managerrecord3->id]);
 | 
        
           |  |  | 104 |         $result = external_api::clean_returnvalue(delete_content::execute_returns(), $result);
 | 
        
           |  |  | 105 |         $this->assertFalse($result['result']);
 | 
        
           |  |  | 106 |         $this->assertCount(1, $result['warnings']);
 | 
        
           |  |  | 107 |         $warning = array_shift($result['warnings']);
 | 
        
           |  |  | 108 |         $this->assertEquals('exception', $warning['warningcode']);
 | 
        
           |  |  | 109 |         $this->assertEquals($managerrecord1->id, $warning['item']);
 | 
        
           |  |  | 110 |         $this->assertEquals(1, $DB->count_records('contentbank_content'));
 | 
        
           |  |  | 111 |     }
 | 
        
           |  |  | 112 | }
 |