| 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 | 
           declare(strict_types = 1);
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           namespace core_completion;
  | 
        
        
            | 
            | 
           20 | 
              | 
        
        
            | 
            | 
           21 | 
           use advanced_testcase;
  | 
        
        
            | 
            | 
           22 | 
           use coding_exception;
  | 
        
        
            | 
            | 
           23 | 
           use moodle_exception;
  | 
        
        
            | 
            | 
           24 | 
           use PHPUnit\Framework\MockObject\MockObject;
  | 
        
        
            | 
            | 
           25 | 
              | 
        
        
            | 
            | 
           26 | 
           /**
  | 
        
        
            | 
            | 
           27 | 
            * Class for unit testing core_completion/activity_custom_completion.
  | 
        
        
            | 
            | 
           28 | 
            *
  | 
        
        
            | 
            | 
           29 | 
            * @package   core_completion
  | 
        
        
            | 
            | 
           30 | 
            * @copyright 2021 Jun Pataleta <jun@moodle.com>
  | 
        
        
            | 
            | 
           31 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           32 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           33 | 
           final class activity_custom_completion_test extends advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           34 | 
              | 
        
        
            | 
            | 
           35 | 
               /**
  | 
        
        
            | 
            | 
           36 | 
                * Fetches a mocked activity_custom_completion instance.
  | 
        
        
            | 
            | 
           37 | 
                *
  | 
        
        
            | 
            | 
           38 | 
                * @param string[] $methods List of methods to mock.
  | 
        
        
            | 
            | 
           39 | 
                * @return activity_custom_completion|MockObject
  | 
        
        
            | 
            | 
           40 | 
                */
  | 
        
        
            | 
            | 
           41 | 
               protected function setup_mock(array $methods) {
  | 
        
        
            | 
            | 
           42 | 
                   return $this->getMockBuilder(activity_custom_completion::class)
  | 
        
        
            | 
            | 
           43 | 
                       ->disableOriginalConstructor()
  | 
        
        
            | 
            | 
           44 | 
                       ->onlyMethods($methods)
  | 
        
        
            | 
            | 
           45 | 
                       ->getMockForAbstractClass();
  | 
        
        
            | 
            | 
           46 | 
               }
  | 
        
        
            | 
            | 
           47 | 
              | 
        
        
            | 
            | 
           48 | 
               /**
  | 
        
        
            | 
            | 
           49 | 
                * Data provider for test_get_overall_completion_state().
  | 
        
        
            | 
            | 
           50 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           51 | 
               public static function overall_completion_state_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           52 | 
                   global $CFG;
  | 
        
        
            | 
            | 
           53 | 
                   require_once($CFG->libdir . '/completionlib.php');
  | 
        
        
            | 
            | 
           54 | 
                   return [
  | 
        
        
            | 
            | 
           55 | 
                       'First incomplete, second complete' => [
  | 
        
        
            | 
            | 
           56 | 
                           ['completionsubmit', 'completioncreate'],
  | 
        
        
            | 
            | 
           57 | 
                           [COMPLETION_INCOMPLETE, COMPLETION_COMPLETE],
  | 
        
        
            | 
            | 
           58 | 
                           1,
  | 
        
        
            | 
            | 
           59 | 
                           COMPLETION_INCOMPLETE,
  | 
        
        
            | 
            | 
           60 | 
                       ],
  | 
        
        
            | 
            | 
           61 | 
                       'First complete, second incomplete' => [
  | 
        
        
            | 
            | 
           62 | 
                           ['completionsubmit', 'completioncreate'],
  | 
        
        
            | 
            | 
           63 | 
                           [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE],
  | 
        
        
            | 
            | 
           64 | 
                           2,
  | 
        
        
            | 
            | 
           65 | 
                           COMPLETION_INCOMPLETE,
  | 
        
        
            | 
            | 
           66 | 
                       ],
  | 
        
        
            | 
            | 
           67 | 
                       'First complete, second failed' => [
  | 
        
        
            | 
            | 
           68 | 
                           ['completionsubmit', 'completioncreate'],
  | 
        
        
            | 
            | 
           69 | 
                           [COMPLETION_COMPLETE, COMPLETION_COMPLETE_FAIL],
  | 
        
        
            | 
            | 
           70 | 
                           2,
  | 
        
        
            | 
            | 
           71 | 
                           COMPLETION_COMPLETE_FAIL,
  | 
        
        
            | 
            | 
           72 | 
                       ],
  | 
        
        
            | 
            | 
           73 | 
                       'First complete, second incomplete, third failed' => [
  | 
        
        
            | 
            | 
           74 | 
                           ['completionsubmit', 'completioncreate'],
  | 
        
        
            | 
            | 
           75 | 
                           [COMPLETION_COMPLETE, COMPLETION_INCOMPLETE, COMPLETION_COMPLETE_FAIL],
  | 
        
        
            | 
            | 
           76 | 
                           2,
  | 
        
        
            | 
            | 
           77 | 
                           COMPLETION_INCOMPLETE,
  | 
        
        
            | 
            | 
           78 | 
                       ],
  | 
        
        
            | 
            | 
           79 | 
                       'All complete' => [
  | 
        
        
            | 
            | 
           80 | 
                           ['completionsubmit', 'completioncreate'],
  | 
        
        
            | 
            | 
           81 | 
                           [COMPLETION_COMPLETE, COMPLETION_COMPLETE],
  | 
        
        
            | 
            | 
           82 | 
                           2,
  | 
        
        
            | 
            | 
           83 | 
                           COMPLETION_COMPLETE,
  | 
        
        
            | 
            | 
           84 | 
                       ],
  | 
        
        
            | 
            | 
           85 | 
                       'No rules' => [
  | 
        
        
            | 
            | 
           86 | 
                           [],
  | 
        
        
            | 
            | 
           87 | 
                           [],
  | 
        
        
            | 
            | 
           88 | 
                           0,
  | 
        
        
            | 
            | 
           89 | 
                           COMPLETION_COMPLETE,
  | 
        
        
            | 
            | 
           90 | 
                       ],
  | 
        
        
            | 
            | 
           91 | 
                   ];
  | 
        
        
            | 
            | 
           92 | 
               }
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
               /**
  | 
        
        
            | 
            | 
           95 | 
                * Test for \core_completion\activity_custom_completion::get_overall_completion_state().
  | 
        
        
            | 
            | 
           96 | 
                *
  | 
        
        
            | 
            | 
           97 | 
                * @dataProvider overall_completion_state_provider
  | 
        
        
            | 
            | 
           98 | 
                * @param string[] $rules The custom completion rules.
  | 
        
        
            | 
            | 
           99 | 
                * @param int[] $rulestates The completion states of these custom completion rules.
  | 
        
        
            | 
            | 
           100 | 
                * @param int $invokecount Expected invoke count of get_state().
  | 
        
        
            | 
            | 
           101 | 
                * @param int $state The expected overall completion state
  | 
        
        
            | 
            | 
           102 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           103 | 
               public function test_get_overall_completion_state(array $rules, array $rulestates, int $invokecount, int $state): void {
  | 
        
        
           | 1 | 
           efrain | 
           104 | 
                   $stub = $this->setup_mock([
  | 
        
        
            | 
            | 
           105 | 
                       'get_available_custom_rules',
  | 
        
        
            | 
            | 
           106 | 
                       'get_state',
  | 
        
        
            | 
            | 
           107 | 
                   ]);
  | 
        
        
            | 
            | 
           108 | 
              | 
        
        
            | 
            | 
           109 | 
                   // Mock activity_custom_completion's get_available_custom_rules() method.
  | 
        
        
            | 
            | 
           110 | 
                   $stub->expects($this->once())
  | 
        
        
            | 
            | 
           111 | 
                       ->method('get_available_custom_rules')
  | 
        
        
            | 
            | 
           112 | 
                       ->willReturn($rules);
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
            | 
            | 
           114 | 
                   // Mock activity_custom_completion's get_state() method.
  | 
        
        
            | 
            | 
           115 | 
                   if ($invokecount > 0) {
  | 
        
        
           | 1441 | 
           ariadna | 
           116 | 
                       $stateinvocations = $this->exactly($invokecount);
  | 
        
        
            | 
            | 
           117 | 
                       $stub->expects($stateinvocations)
  | 
        
        
           | 1 | 
           efrain | 
           118 | 
                           ->method('get_state')
  | 
        
        
           | 1441 | 
           ariadna | 
           119 | 
                           ->willReturnCallback(function ($rule) use ($stateinvocations, $rules, $rulestates) {
  | 
        
        
            | 
            | 
           120 | 
                               $index = self::getInvocationCount($stateinvocations) - 1;
  | 
        
        
            | 
            | 
           121 | 
                               $this->assertEquals($rules[$index], $rule);
  | 
        
        
            | 
            | 
           122 | 
                               return $rulestates[$index];
  | 
        
        
            | 
            | 
           123 | 
                           });
  | 
        
        
           | 1 | 
           efrain | 
           124 | 
                   } else {
  | 
        
        
            | 
            | 
           125 | 
                       $stub->expects($this->never())
  | 
        
        
            | 
            | 
           126 | 
                           ->method('get_state');
  | 
        
        
            | 
            | 
           127 | 
                   }
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
                   $this->assertEquals($state, $stub->get_overall_completion_state());
  | 
        
        
            | 
            | 
           130 | 
               }
  | 
        
        
            | 
            | 
           131 | 
              | 
        
        
            | 
            | 
           132 | 
               /**
  | 
        
        
            | 
            | 
           133 | 
                * Data provider for test_validate_rule().
  | 
        
        
            | 
            | 
           134 | 
                *
  | 
        
        
            | 
            | 
           135 | 
                * @return array[]
  | 
        
        
            | 
            | 
           136 | 
                */
  | 
        
        
           | 1441 | 
           ariadna | 
           137 | 
               public static function validate_rule_provider(): array {
  | 
        
        
           | 1 | 
           efrain | 
           138 | 
                   return [
  | 
        
        
            | 
            | 
           139 | 
                       'Not defined' => [
  | 
        
        
            | 
            | 
           140 | 
                           false, true, coding_exception::class
  | 
        
        
            | 
            | 
           141 | 
                       ],
  | 
        
        
            | 
            | 
           142 | 
                       'Not available' => [
  | 
        
        
            | 
            | 
           143 | 
                           true, false, moodle_exception::class
  | 
        
        
            | 
            | 
           144 | 
                       ],
  | 
        
        
            | 
            | 
           145 | 
                       'Defined and available' => [
  | 
        
        
            | 
            | 
           146 | 
                           true, true, null
  | 
        
        
            | 
            | 
           147 | 
                       ],
  | 
        
        
            | 
            | 
           148 | 
                   ];
  | 
        
        
            | 
            | 
           149 | 
               }
  | 
        
        
            | 
            | 
           150 | 
              | 
        
        
            | 
            | 
           151 | 
               /**
  | 
        
        
            | 
            | 
           152 | 
                * Test for validate_rule()
  | 
        
        
            | 
            | 
           153 | 
                *
  | 
        
        
            | 
            | 
           154 | 
                * @dataProvider validate_rule_provider
  | 
        
        
            | 
            | 
           155 | 
                * @param bool $defined is_defined()'s mocked return value.
  | 
        
        
            | 
            | 
           156 | 
                * @param bool $available is_available()'s mocked return value.
  | 
        
        
            | 
            | 
           157 | 
                * @param string|null $expectedexception Expected expectation class name.
  | 
        
        
            | 
            | 
           158 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           159 | 
               public function test_validate_rule(bool $defined, bool $available, ?string $expectedexception): void {
  | 
        
        
           | 1 | 
           efrain | 
           160 | 
                   $stub = $this->setup_mock([
  | 
        
        
            | 
            | 
           161 | 
                       'is_defined',
  | 
        
        
            | 
            | 
           162 | 
                       'is_available'
  | 
        
        
            | 
            | 
           163 | 
                   ]);
  | 
        
        
            | 
            | 
           164 | 
              | 
        
        
            | 
            | 
           165 | 
                   // Mock activity_custom_completion's is_defined() method.
  | 
        
        
            | 
            | 
           166 | 
                   $stub->expects($this->any())
  | 
        
        
            | 
            | 
           167 | 
                       ->method('is_defined')
  | 
        
        
            | 
            | 
           168 | 
                       ->willReturn($defined);
  | 
        
        
            | 
            | 
           169 | 
              | 
        
        
            | 
            | 
           170 | 
                   // Mock activity_custom_completion's is_available() method.
  | 
        
        
            | 
            | 
           171 | 
                   $stub->expects($this->any())
  | 
        
        
            | 
            | 
           172 | 
                       ->method('is_available')
  | 
        
        
            | 
            | 
           173 | 
                       ->willReturn($available);
  | 
        
        
            | 
            | 
           174 | 
              | 
        
        
            | 
            | 
           175 | 
                   if ($expectedexception) {
  | 
        
        
            | 
            | 
           176 | 
                       $this->expectException($expectedexception);
  | 
        
        
            | 
            | 
           177 | 
                   }
  | 
        
        
            | 
            | 
           178 | 
                   $stub->validate_rule('customcompletionrule');
  | 
        
        
            | 
            | 
           179 | 
               }
  | 
        
        
            | 
            | 
           180 | 
              | 
        
        
            | 
            | 
           181 | 
               /**
  | 
        
        
            | 
            | 
           182 | 
                * Test for is_available().
  | 
        
        
            | 
            | 
           183 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           184 | 
               public function test_is_available(): void {
  | 
        
        
           | 1 | 
           efrain | 
           185 | 
                   $stub = $this->setup_mock([
  | 
        
        
            | 
            | 
           186 | 
                       'get_available_custom_rules',
  | 
        
        
            | 
            | 
           187 | 
                   ]);
  | 
        
        
            | 
            | 
           188 | 
              | 
        
        
            | 
            | 
           189 | 
                   // Mock activity_custom_completion's get_available_custom_rules() method.
  | 
        
        
            | 
            | 
           190 | 
                   $stub->expects($this->any())
  | 
        
        
            | 
            | 
           191 | 
                       ->method('get_available_custom_rules')
  | 
        
        
            | 
            | 
           192 | 
                       ->willReturn(['rule1', 'rule2']);
  | 
        
        
            | 
            | 
           193 | 
              | 
        
        
            | 
            | 
           194 | 
                   // Rule is available.
  | 
        
        
            | 
            | 
           195 | 
                   $this->assertTrue($stub->is_available('rule1'));
  | 
        
        
            | 
            | 
           196 | 
              | 
        
        
            | 
            | 
           197 | 
                   // Rule is not available.
  | 
        
        
            | 
            | 
           198 | 
                   $this->assertFalse($stub->is_available('rule'));
  | 
        
        
            | 
            | 
           199 | 
               }
  | 
        
        
            | 
            | 
           200 | 
           }
  |