|
| 1 | +<?php |
| 2 | +// This file is part of Moodle - https://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 <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +namespace block_course_contents; |
| 18 | + |
| 19 | +/** |
| 20 | + * The autotitle feature test. |
| 21 | + * |
| 22 | + * @package block_course_contents |
| 23 | + * @copyright 2021 David Mudrák <david@moodle.com> |
| 24 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + */ |
| 26 | +class autotitle_test extends \advanced_testcase { |
| 27 | + |
| 28 | + /** |
| 29 | + * Test extracting suitable title from the summary HTML text. |
| 30 | + * |
| 31 | + * @dataProvider extract_title_data |
| 32 | + * @param string $summary |
| 33 | + * @param string $title |
| 34 | + */ |
| 35 | + public function test_extract_title(string $summary, string $title) { |
| 36 | + $this->assertEquals($title, autotitle::extract_title($summary)); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Provides data for {@see self::test_extract_title()}. |
| 41 | + * |
| 42 | + * @return array |
| 43 | + */ |
| 44 | + public function extract_title_data(): array { |
| 45 | + return [ |
| 46 | + 'Plain text' => [ |
| 47 | + 'summary' => 'Welcome to this course!', |
| 48 | + 'title' => 'Welcome to this course!', |
| 49 | + ], |
| 50 | + 'Heading' => [ |
| 51 | + 'summary' => '<h3>Welcome!</h3><p>In this course, you will learn a lot.</p>', |
| 52 | + 'title' => 'Welcome!', |
| 53 | + ], |
| 54 | + 'Bold' => [ |
| 55 | + 'summary' => '<p><b>Welcome!</b> In this course, you will learn a lot.</p>', |
| 56 | + 'title' => 'Welcome!', |
| 57 | + ], |
| 58 | + 'First non-empty' => [ |
| 59 | + 'summary' => '<p class="test"><strong> </strong><i></i></p><p></p><br /><p class="test">Hello</p>', |
| 60 | + 'title' => 'Hello', |
| 61 | + ], |
| 62 | + 'Siblings' => [ |
| 63 | + 'summary' => '<div><span>First</span><span>Two</span></div>', |
| 64 | + 'title' => 'First', |
| 65 | + ], |
| 66 | + 'No actual text content, just HTML' => [ |
| 67 | + 'summary' => "<br />\n\t<p>\n</p>", |
| 68 | + 'title' => '', |
| 69 | + ], |
| 70 | + 'Empty string' => [ |
| 71 | + 'summary' => '', |
| 72 | + 'title' => '', |
| 73 | + ], |
| 74 | + 'Break lines' => [ |
| 75 | + 'summary' => 'Hello <br> students', |
| 76 | + 'title' => 'Hello', |
| 77 | + ], |
| 78 | + ]; |
| 79 | + } |
| 80 | +} |
0 commit comments