diff --git a/course/format/classes/local/overview/overviewitem.php b/course/format/classes/local/overview/overviewitem.php index ddf7f0d1552..b751dc46e0c 100644 --- a/course/format/classes/local/overview/overviewitem.php +++ b/course/format/classes/local/overview/overviewitem.php @@ -139,36 +139,44 @@ class overviewitem { * Items can utilize either a renderable object or a pre-rendered string as their content. * * @param string|renderable|null $content + * @return $this */ - public function set_content(string|renderable|null $content): void { + public function set_content(string|renderable|null $content): static { $this->content = $content; + return $this; } /** * Sets the preferred text alignment of the item. * * @param text_align $textalign + * @return $this */ - public function set_text_align(text_align $textalign): void { + public function set_text_align(text_align $textalign): static { $this->textalign = $textalign; + return $this; } /** * Sets the value of the overview item. * * @param int|string|bool|null $value + * @return $this */ - public function set_value(int|string|bool|null $value): void { + public function set_value(int|string|bool|null $value): static { $this->value = $value; + return $this; } /** * Sets the name of the overview item. * * @param string $name + * @return $this */ - public function set_name(string $name): void { + public function set_name(string $name): static { $this->name = $name; + return $this; } /** @@ -176,9 +184,11 @@ class overviewitem { * * @param int $alertcount * @param string $alertlabel + * @return $this */ - public function set_alert(int $alertcount, string $alertlabel): void { + public function set_alert(int $alertcount, string $alertlabel): static { $this->alertcount = $alertcount; $this->alertlabel = $alertlabel; + return $this; } } diff --git a/course/format/tests/local/overview/overviewitem_test.php b/course/format/tests/local/overview/overviewitem_test.php new file mode 100644 index 00000000000..ef80b2943dc --- /dev/null +++ b/course/format/tests/local/overview/overviewitem_test.php @@ -0,0 +1,106 @@ +. + +namespace core_courseformat\local\overview; + +use core\output\local\properties\text_align; + +/** + * Tests for overviewitem. + * + * @package core_courseformat + * @category test + * @copyright 2025 Ferran Recio + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \core_courseformat\local\overview\overviewitem + */ +final class overviewitem_test extends \advanced_testcase { + /** + * Tests the constructor. + * + * @covers ::__construct + * @covers ::get_name + * @covers ::get_value + * @covers ::get_content + * @covers ::get_text_align + * @covers ::get_alert_count + * @covers ::get_alert_label + */ + public function test_constructor(): void { + $name = 'Activity name'; + $value = 1; + $content = 'Activity content'; + $textalign = text_align::CENTER; + $alertcount = 1; + $alertlabel = 'Alert label'; + + $item = new overviewitem($name, $value, $content, $textalign, $alertcount, $alertlabel); + + $this->assertEquals($name, $item->get_name()); + $this->assertEquals($value, $item->get_value()); + $this->assertEquals($content, $item->get_content()); + $this->assertEquals($textalign, $item->get_text_align()); + $this->assertEquals($alertcount, $item->get_alert_count()); + $this->assertEquals($alertlabel, $item->get_alert_label()); + } + + /** + * Test chained setters. + * + * @covers ::set_name + * @covers ::set_value + * @covers ::set_content + * @covers ::set_text_align + * @covers ::set_alert_count + * @covers ::set_alert_label + * @covers ::get_name + * @covers ::get_value + * @covers ::get_content + * @covers ::get_text_align + * @covers ::get_alert_count + * @covers ::get_alert_label + */ + public function test_setters(): void { + $item = new overviewitem('Sample', 1, 'Content', text_align::CENTER, 1, 'Alert label'); + + $this->assertEquals('Sample', $item->get_name()); + $this->assertEquals(1, $item->get_value()); + $this->assertEquals('Content', $item->get_content()); + $this->assertEquals(text_align::CENTER, $item->get_text_align()); + $this->assertEquals(1, $item->get_alert_count()); + $this->assertEquals('Alert label', $item->get_alert_label()); + + $name = 'New activity name'; + $value = 2; + $content = 'New activity content'; + $textalign = text_align::END; + $alertcount = 2; + $alertlabel = 'New alert label'; + + $item->set_name($name) + ->set_value($value) + ->set_content($content) + ->set_text_align($textalign) + ->set_alert($alertcount, $alertlabel); + + $this->assertEquals($name, $item->get_name()); + $this->assertEquals($value, $item->get_value()); + $this->assertEquals($content, $item->get_content()); + $this->assertEquals($textalign, $item->get_text_align()); + $this->assertEquals($alertcount, $item->get_alert_count()); + $this->assertEquals($alertlabel, $item->get_alert_label()); + } +}