| 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 | 
            * booktool_importhtml tests.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package    booktool_importhtml
  | 
        
        
            | 
            | 
           21 | 
            * @category   phpunit
  | 
        
        
            | 
            | 
           22 | 
            * @copyright  2013 Frédéric Massart
  | 
        
        
            | 
            | 
           23 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           24 | 
            */
  | 
        
        
            | 
            | 
           25 | 
           namespace booktool_importhtml;
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           28 | 
           global $CFG;
  | 
        
        
            | 
            | 
           29 | 
              | 
        
        
            | 
            | 
           30 | 
           require_once($CFG->dirroot.'/mod/book/tool/importhtml/locallib.php');
  | 
        
        
            | 
            | 
           31 | 
              | 
        
        
            | 
            | 
           32 | 
           /**
  | 
        
        
            | 
            | 
           33 | 
            * booktool_importhtml tests class.
  | 
        
        
            | 
            | 
           34 | 
            *
  | 
        
        
            | 
            | 
           35 | 
            * @package    booktool_importhtml
  | 
        
        
            | 
            | 
           36 | 
            * @category   phpunit
  | 
        
        
            | 
            | 
           37 | 
            * @copyright  2013 Frédéric Massart
  | 
        
        
            | 
            | 
           38 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           39 | 
            */
  | 
        
        
           | 1441 | 
           ariadna | 
           40 | 
           final class locallib_test extends \advanced_testcase {
  | 
        
        
           | 1 | 
           efrain | 
           41 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           42 | 
               /** @var object */
  | 
        
        
            | 
            | 
           43 | 
               private object $book;
  | 
        
        
            | 
            | 
           44 | 
               /** @var object */
  | 
        
        
            | 
            | 
           45 | 
               private object $context;
  | 
        
        
            | 
            | 
           46 | 
               /** @var object */
  | 
        
        
            | 
            | 
           47 | 
               private object $record;
  | 
        
        
            | 
            | 
           48 | 
              | 
        
        
           | 1 | 
           efrain | 
           49 | 
               public function setUp(): void {
  | 
        
        
           | 1441 | 
           ariadna | 
           50 | 
                   parent::setUp();
  | 
        
        
           | 1 | 
           efrain | 
           51 | 
                   $this->resetAfterTest();
  | 
        
        
           | 1441 | 
           ariadna | 
           52 | 
              | 
        
        
            | 
            | 
           53 | 
                   $course = $this->getDataGenerator()->create_course();
  | 
        
        
            | 
            | 
           54 | 
                   $this->book = $this->getDataGenerator()->create_module('book', ['course' => $course->id]);
  | 
        
        
            | 
            | 
           55 | 
                   $this->context = \context_module::instance($this->book->cmid);
  | 
        
        
            | 
            | 
           56 | 
                   $this->record = (object) [
  | 
        
        
            | 
            | 
           57 | 
                       'contextid' => $this->context->id,
  | 
        
        
            | 
            | 
           58 | 
                       'component' => 'phpunit',
  | 
        
        
            | 
            | 
           59 | 
                       'filearea'  => 'test',
  | 
        
        
            | 
            | 
           60 | 
                       'itemid'    => 0,
  | 
        
        
            | 
            | 
           61 | 
                       'filepath'  => '/',
  | 
        
        
            | 
            | 
           62 | 
                   ];
  | 
        
        
           | 1 | 
           efrain | 
           63 | 
               }
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           65 | 
               /**
  | 
        
        
            | 
            | 
           66 | 
                * Tests the events within toolbook_importhtml_import_chapters
  | 
        
        
            | 
            | 
           67 | 
                * @covers ::toolbook_importhtml_import_chapters
  | 
        
        
            | 
            | 
           68 | 
                */
  | 
        
        
           | 11 | 
           efrain | 
           69 | 
               public function test_import_chapters_events(): void {
  | 
        
        
           | 1 | 
           efrain | 
           70 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           71 | 
                   $this->record->filename = 'chapters.zip';
  | 
        
        
            | 
            | 
           72 | 
                   $file = get_file_storage()->create_file_from_pathname(
  | 
        
        
            | 
            | 
           73 | 
                       $this->record,
  | 
        
        
            | 
            | 
           74 | 
                       self::get_fixture_path(__NAMESPACE__, 'chapters.zip')
  | 
        
        
            | 
            | 
           75 | 
                   );
  | 
        
        
           | 1 | 
           efrain | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   // Importing the chapters.
  | 
        
        
            | 
            | 
           78 | 
                   $sink = $this->redirectEvents();
  | 
        
        
           | 1441 | 
           ariadna | 
           79 | 
                   toolbook_importhtml_import_chapters($file, 2, $this->book, $this->context, false);
  | 
        
        
           | 1 | 
           efrain | 
           80 | 
                   $events = $sink->get_events();
  | 
        
        
            | 
            | 
           81 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           82 | 
                   // Checking the results up to the triggered event.
  | 
        
        
           | 1 | 
           efrain | 
           83 | 
                   $this->assertCount(5, $events);
  | 
        
        
            | 
            | 
           84 | 
                   foreach ($events as $event) {
  | 
        
        
            | 
            | 
           85 | 
                       $this->assertInstanceOf('\mod_book\event\chapter_created', $event);
  | 
        
        
           | 1441 | 
           ariadna | 
           86 | 
                       $this->assertEquals($this->context, $event->get_context());
  | 
        
        
           | 1 | 
           efrain | 
           87 | 
                       $chapter = $event->get_record_snapshot('book_chapters', $event->objectid);
  | 
        
        
            | 
            | 
           88 | 
                       $this->assertNotEmpty($chapter);
  | 
        
        
            | 
            | 
           89 | 
                       $this->assertEventContextNotUsed($event);
  | 
        
        
            | 
            | 
           90 | 
                   }
  | 
        
        
            | 
            | 
           91 | 
               }
  | 
        
        
            | 
            | 
           92 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           93 | 
               /**
  | 
        
        
            | 
            | 
           94 | 
                * Tests the conversion of (anchored) links within toolbook_importhtml_import_chapters
  | 
        
        
            | 
            | 
           95 | 
                * @covers ::toolbook_importhtml_import_chapters
  | 
        
        
            | 
            | 
           96 | 
                */
  | 
        
        
            | 
            | 
           97 | 
               public function test_import_chapters_links(): void {
  | 
        
        
            | 
            | 
           98 | 
                   global $DB;
  | 
        
        
            | 
            | 
           99 | 
              | 
        
        
            | 
            | 
           100 | 
                   $this->record->filename = 'chapters_links.zip';
  | 
        
        
            | 
            | 
           101 | 
                   $file = get_file_storage()->create_file_from_pathname(
  | 
        
        
            | 
            | 
           102 | 
                       $this->record,
  | 
        
        
            | 
            | 
           103 | 
                       self::get_fixture_path(__NAMESPACE__, 'chapters_links.zip')
  | 
        
        
            | 
            | 
           104 | 
                   );
  | 
        
        
            | 
            | 
           105 | 
              | 
        
        
            | 
            | 
           106 | 
                   toolbook_importhtml_import_chapters($file, 2, $this->book, $this->context, false);
  | 
        
        
            | 
            | 
           107 | 
              | 
        
        
            | 
            | 
           108 | 
                   $chapters = $DB->get_records('book_chapters', ['bookid' => $this->book->id]);
  | 
        
        
            | 
            | 
           109 | 
                   foreach ($chapters as $chapter) {
  | 
        
        
            | 
            | 
           110 | 
                       $this->assertStringNotContainsString('.html', $chapter->content);
  | 
        
        
            | 
            | 
           111 | 
                       $this->assertStringNotContainsString('.html#anchor', $chapter->content);
  | 
        
        
            | 
            | 
           112 | 
                       $this->assertMatchesRegularExpression('/chapterid=[0-9]*"/i', $chapter->content);
  | 
        
        
            | 
            | 
           113 | 
                       $this->assertMatchesRegularExpression('/chapterid=[0-9]*#anchor"/i', $chapter->content);
  | 
        
        
            | 
            | 
           114 | 
                   }
  | 
        
        
            | 
            | 
           115 | 
               }
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
           | 1 | 
           efrain | 
           117 | 
           }
  |