Skip to content

Commit

Permalink
MDL-73983 reportbuilder: fix pre-defined action title attribute.
Browse files Browse the repository at this point in the history
Since switching to report actions being displayed via action menus
in 48a6e92, pre-defined action titles weren't displayed.
  • Loading branch information
paulholden authored and dravek committed Feb 28, 2022
1 parent 8cc2a86 commit 0ead4ce
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
14 changes: 10 additions & 4 deletions reportbuilder/classes/local/report/action.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ public function add_callback(callable $callback): self {
}

/**
* Return renderer action icon suitable for output
*
* @uses core_renderer::action_icon()
* Return rendered action link suitable for output, or null if the action cannot be displayed (because one of it's callbacks
* returned false, {@see add_callback})
*
* @param stdClass $row
* @return string|null
Expand Down Expand Up @@ -130,7 +129,14 @@ public function get_action_link(stdClass $row): ?string {
$this->attributes['data-popup-action'] = json_encode(new popup_action('click', $url));
}

return $OUTPUT->action_link($url, $this->title, null, self::replace_placeholders($this->attributes, $row), $this->icon);
// Interpolate any placeholders with correct values.
$attributes = self::replace_placeholders($this->attributes, $row);

// Ensure title attribute isn't duplicated.
$title = $attributes['title'];
unset($attributes['title']);

return $OUTPUT->action_link($url, $title, null, $attributes, $this->icon);
}

/**
Expand Down
1 change: 1 addition & 0 deletions reportbuilder/classes/table/system_report_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ private function format_row_actions(stdClass $row): string {
$menu = new action_menu();
$menu->set_menu_trigger($OUTPUT->pix_icon('a/setting', get_string('actions', 'core_reportbuilder')));
foreach ($this->report->get_actions() as $action) {
// Ensure the action link can be displayed for the current row.
$actionlink = $action->get_action_link($row);
if ($actionlink) {
$menu->add($actionlink);
Expand Down
46 changes: 46 additions & 0 deletions reportbuilder/tests/local/report/action_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace core_reportbuilder\local\report;

use advanced_testcase;
use lang_string;
use moodle_url;
use pix_icon;
use stdClass;
Expand Down Expand Up @@ -57,6 +58,51 @@ public function test_add_callback_false(): void {
$this->assertNull($action->get_action_link(new stdClass()));
}

/**
* Data provider for {@see test_action_title}
*
* @return array[]
*/
public function action_title_provider(): array {
$title = new lang_string('yes');
return [
'Specified via constructor' => ['', [], $title],
'Specified via pix icon' => [(string) $title],
'Specified via attributes' => ['', ['title' => $title]],
'Specified via attributes placeholder' => ['', ['title' => ':title'], null, ['title' => $title]],
];
}

/**
* Test action title is correct
*
* @param string $pixiconalt
* @param array $attributes
* @param lang_string|null $title
* @param array $row
*
* @dataProvider action_title_provider
*/
public function test_action_title(
string $pixiconalt,
array $attributes = [],
?lang_string $title = null,
array $row = []
): void {

$action = new action(
new moodle_url('#'),
new pix_icon('t/edit', $pixiconalt),
$attributes,
false,
$title
);

// Assert correct title appears inside action link, after the icon.
$actionlink = $action->get_action_link((object) $row);
$this->assertStringEndsWith('</i>Yes</a>', $actionlink);
}

/**
* Test that action link URL parameters have placeholders replaced
*/
Expand Down

0 comments on commit 0ead4ce

Please sign in to comment.