| 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 | 
           namespace tool_messageinbound;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use core_privacy\tests\provider_testcase;
  | 
        
        
            | 
            | 
           20 | 
           use core_privacy\local\request\approved_contextlist;
  | 
        
        
            | 
            | 
           21 | 
           use core_privacy\local\request\transform;
  | 
        
        
            | 
            | 
           22 | 
           use core_privacy\local\request\writer;
  | 
        
        
            | 
            | 
           23 | 
           use tool_messageinbound\privacy\provider;
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
            | 
            | 
           25 | 
           /**
  | 
        
        
            | 
            | 
           26 | 
            * Manager testcase class.
  | 
        
        
            | 
            | 
           27 | 
            *
  | 
        
        
            | 
            | 
           28 | 
            * @package    tool_messageinbound
  | 
        
        
            | 
            | 
           29 | 
            * @category   test
  | 
        
        
            | 
            | 
           30 | 
            * @copyright  2018 Frédéric Massart
  | 
        
        
            | 
            | 
           31 | 
            * @author     Frédéric Massart <fred@branchup.tech>
  | 
        
        
            | 
            | 
           32 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           33 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           34 | 
           final class manager_test extends provider_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           35 | 
              | 
        
        
            | 
            | 
           36 | 
               public function setUp(): void {
  | 
        
        
            | 
            | 
           37 | 
                   global $CFG;
  | 
        
        
           | 1441 | 
           ariadna | 
           38 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           39 | 
                   $this->resetAfterTest();
  | 
        
        
            | 
            | 
           40 | 
              | 
        
        
            | 
            | 
           41 | 
                   // Pretend the system is enabled.
  | 
        
        
            | 
            | 
           42 | 
                   $CFG->messageinbound_enabled = true;
  | 
        
        
            | 
            | 
           43 | 
                   $CFG->messageinbound_mailbox = 'mailbox';
  | 
        
        
            | 
            | 
           44 | 
                   $CFG->messageinbound_domain = 'example.com';
  | 
        
        
            | 
            | 
           45 | 
               }
  | 
        
        
            | 
            | 
           46 | 
              | 
        
        
           | 11 | 
           efrain | 
           47 | 
               public function test_tidy_old_verification_failures(): void {
  | 
        
        
           | 1 | 
           efrain | 
           48 | 
                   global $DB;
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
            | 
            | 
           50 | 
                   $now = time();
  | 
        
        
            | 
            | 
           51 | 
                   $stale = $now - DAYSECS - 1;    // Make a second older because PHP Unit is too damn fast!!
  | 
        
        
            | 
            | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
                   $this->create_messagelist(['timecreated' => $now]);
  | 
        
        
            | 
            | 
           54 | 
                   $this->create_messagelist(['timecreated' => $now - HOURSECS]);
  | 
        
        
            | 
            | 
           55 | 
                   $this->create_messagelist(['timecreated' => $stale]);
  | 
        
        
            | 
            | 
           56 | 
                   $this->create_messagelist(['timecreated' => $stale - HOURSECS]);
  | 
        
        
            | 
            | 
           57 | 
                   $this->create_messagelist(['timecreated' => $stale - YEARSECS]);
  | 
        
        
            | 
            | 
           58 | 
              | 
        
        
            | 
            | 
           59 | 
                   $this->assertEquals(5, $DB->count_records('messageinbound_messagelist', []));
  | 
        
        
            | 
            | 
           60 | 
                   $this->assertEquals(3, $DB->count_records_select('messageinbound_messagelist', 'timecreated < :t', ['t' => $stale + 1]));
  | 
        
        
            | 
            | 
           61 | 
              | 
        
        
            | 
            | 
           62 | 
                   $manager = new \tool_messageinbound\manager();
  | 
        
        
            | 
            | 
           63 | 
                   $manager->tidy_old_verification_failures();
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
                   $this->assertEquals(2, $DB->count_records('messageinbound_messagelist', []));
  | 
        
        
            | 
            | 
           66 | 
                   $this->assertEquals(0, $DB->count_records_select('messageinbound_messagelist', 'timecreated < :t', ['t' => $stale + 1]));
  | 
        
        
            | 
            | 
           67 | 
               }
  | 
        
        
            | 
            | 
           68 | 
              | 
        
        
            | 
            | 
           69 | 
               /**
  | 
        
        
            | 
            | 
           70 | 
                * Create a message to validate.
  | 
        
        
            | 
            | 
           71 | 
                *
  | 
        
        
            | 
            | 
           72 | 
                * @param array $params The params.
  | 
        
        
            | 
            | 
           73 | 
                * @return stdClass
  | 
        
        
            | 
            | 
           74 | 
                */
  | 
        
        
            | 
            | 
           75 | 
               protected function create_messagelist(array $params) {
  | 
        
        
            | 
            | 
           76 | 
                   global $DB, $USER;
  | 
        
        
            | 
            | 
           77 | 
                   $record = (object) array_merge([
  | 
        
        
            | 
            | 
           78 | 
                       'messageid' => 'abc',
  | 
        
        
            | 
            | 
           79 | 
                       'userid' => $USER->id,
  | 
        
        
            | 
            | 
           80 | 
                       'address' => 'text@example.com',
  | 
        
        
            | 
            | 
           81 | 
                       'timecreated' => time(),
  | 
        
        
            | 
            | 
           82 | 
                   ], $params);
  | 
        
        
            | 
            | 
           83 | 
                   $record->id = $DB->insert_record('messageinbound_messagelist', $record);
  | 
        
        
            | 
            | 
           84 | 
                   return $record;
  | 
        
        
            | 
            | 
           85 | 
               }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           }
  |