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_reportbuilder\local\report; 20 21 use advanced_testcase; 22 use lang_string; 23 use moodle_url; 24 use pix_icon; 25 use stdClass; 26 27 /** 28 * Unit tests for a report action 29 * 30 * @package core_reportbuilder 31 * @covers \core_reportbuilder\local\report\action 32 * @copyright 2021 Paul Holden <paulh@moodle.com> 33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 34 */ 35 class action_test extends advanced_testcase { 36 37 /** 38 * Test adding a callback that returns true 39 */ 40 public function test_add_callback_true(): void { 41 $action = $this->create_action() 42 ->add_callback(static function(stdClass $row): bool { 43 return true; 44 }); 45 46 $this->assertNotNull($action->get_action_link(new stdClass())); 47 } 48 49 /** 50 * Test adding a callback that returns false 51 */ 52 public function test_add_callback_false(): void { 53 $action = $this->create_action() 54 ->add_callback(static function(stdClass $row): bool { 55 return false; 56 }); 57 58 $this->assertNull($action->get_action_link(new stdClass())); 59 } 60 61 /** 62 * Data provider for {@see test_action_title} 63 * 64 * @return array[] 65 */ 66 public function action_title_provider(): array { 67 $title = new lang_string('yes'); 68 return [ 69 'Specified via constructor' => ['', [], $title], 70 'Specified via pix icon' => [(string) $title], 71 'Specified via attributes' => ['', ['title' => $title]], 72 'Specified via attributes placeholder' => ['', ['title' => ':title'], null, ['title' => $title]], 73 ]; 74 } 75 76 /** 77 * Test action title is correct 78 * 79 * @param string $pixiconalt 80 * @param array $attributes 81 * @param lang_string|null $title 82 * @param array $row 83 * 84 * @dataProvider action_title_provider 85 */ 86 public function test_action_title( 87 string $pixiconalt, 88 array $attributes = [], 89 ?lang_string $title = null, 90 array $row = [] 91 ): void { 92 93 $action = new action( 94 new moodle_url('#'), 95 new pix_icon('t/edit', $pixiconalt), 96 $attributes, 97 false, 98 $title 99 ); 100 101 // Assert correct title appears inside action link, after the icon. 102 $actionlink = $action->get_action_link((object) $row); 103 $this->assertEquals('Yes', $actionlink->text); 104 } 105 106 /** 107 * Test that action link URL parameters have placeholders replaced 108 */ 109 public function test_get_action_link_url_parameters(): void { 110 $action = $this->create_action(['id' => ':id', 'action' => 'edit']); 111 $actionlink = $action->get_action_link((object) ['id' => 42]); 112 113 // This is the action URL we expect. 114 $expectedactionurl = (new moodle_url('/', ['id' => 42, 'action' => 'edit']))->out(false); 115 $this->assertEquals($expectedactionurl, $actionlink->url->out(false)); 116 } 117 118 /** 119 * Test that action link attributes have placeholders replaced 120 */ 121 public function test_get_action_link_attributes(): void { 122 $action = $this->create_action([], ['data-id' => ':id', 'data-action' => 'edit']); 123 $actionlink = $action->get_action_link((object) ['id' => 42]); 124 125 // We expect each of these attributes to exist. 126 $expectedattributes = [ 127 'data-id' => 42, 128 'data-action' => 'edit', 129 ]; 130 foreach ($expectedattributes as $key => $value) { 131 $this->assertEquals($value, $actionlink->attributes[$key]); 132 } 133 } 134 135 /** 136 * Helper method to create an action instance 137 * 138 * @param array $urlparams 139 * @param array $attributes 140 * @return action 141 */ 142 private function create_action(array $urlparams = [], array $attributes = []): action { 143 return new action( 144 new moodle_url('/', $urlparams), 145 new pix_icon('t/edit', get_string('edit')), 146 $attributes 147 ); 148 } 149 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body