diff --git a/test/php/library/Icinga/Web/Dashboard/BaseDashboardTestCaseTest.php b/test/php/library/Icinga/Web/Dashboard/BaseDashboardTestCaseTest.php new file mode 100644 index 000000000..d7e7f8b8d --- /dev/null +++ b/test/php/library/Icinga/Web/Dashboard/BaseDashboardTestCaseTest.php @@ -0,0 +1,134 @@ +dashboard->addEntry($this->getTestHome()); + + $this->assertTrue( + $this->dashboard->hasEntry(self::TEST_HOME), + 'DashboardEntries::addEntry() could not add a Dashboard entry' + ); + } + + /** + * @depends testWhetherAddEntryAddsAnEntry + */ + public function testWhetherAddEntryAddsDifferentEntries() + { + $this->dashboard->addEntry($this->getTestHome()); + $this->dashboard->addEntry($this->getTestHome('Second Home')); + $this->dashboard->addEntry($this->getTestHome('Third Home')); + + $this->assertCount( + 3, + $this->dashboard->getEntries(), + 'DashboardEntries::addEntry() could not add different Dashboard entries' + ); + } + + /** + * @depends testWhetherAddEntryAddsDifferentEntries + */ + public function testMergeEntryWithSameEntryName() + { + $this->dashboard->addEntry($this->getTestHome()); + $this->dashboard->addEntry($this->getTestHome('Second Home')); + $this->dashboard->addEntry($this->getTestHome('Second Home')); + + $this->assertCount( + 2, + $this->dashboard->getEntries(), + 'DashboardEntries::addEntry() could not merge same Dashboard entries' + ); + } + + /** + * @depends testMergeEntryWithSameEntryName + */ + public function testWhetherGetEntriesReturnsExpectedEntries() + { + $this->dashboard->addEntry($this->getTestHome()); + + $this->assertCount( + 1, + $this->dashboard->getEntries(), + 'DashboardEntries::getEntries() returns unexpected dashboard entries' + ); + } + + public function testeWhetherGetEntryThrowsAnExceptionOnNotExistentEntryName() + { + $this->expectException(ProgrammingError::class); + + $this->dashboard->getEntry('test'); + } + + /** + * @depends testeWhetherGetEntryThrowsAnExceptionOnNotExistentEntryName + */ + public function testWhetherGetEntryGetsAnEntryByName() + { + $this->dashboard->addEntry($this->getTestHome()); + + $this->assertEquals( + self::TEST_HOME, + $this->dashboard->getEntry(self::TEST_HOME)->getName(), + 'DashboardEntries:getEntry() could not return Dashboard entry by name' + ); + } + + /** + * @depends testMergeEntryWithSameEntryName + */ + public function testWhetherHasEntriesHasNoEntries() + { + $this->assertFalse( + $this->dashboard->hasEntries(), + 'DashboardEntries::hasEntries() has Dashboard entries but should not' + ); + } + + /** + * @depends testWhetherHasEntriesHasNoEntries + */ + public function testWhetherHasEntriesHasEntries() + { + $this->dashboard->addEntry($this->getTestHome()); + + $this->assertTrue( + $this->dashboard->hasEntries(), + 'DashboardEntries::hasEntries() could not return valid expectation' + ); + } + + /** + * @depends testWhetherHasEntriesHasEntries + */ + public function testWhetherGetEntryKeyTitleArrayReturnFormedArray() + { + $this->dashboard->addEntry(($this->getTestHome())->setTitle('First Home')); + $this->dashboard->addEntry(($this->getTestHome('Test2')->setTitle('Second Home'))); + $this->dashboard->addEntry(($this->getTestHome('Test3')->setTitle('Third Home'))); + + $expected = [ + self::TEST_HOME => 'First Home', + 'Test2' => 'Second Home', + 'Test3' => 'Third Home' + ]; + + $this->assertEquals( + $expected, + $this->dashboard->getEntryKeyTitleArr(), + 'DashboardEntries::getEntryKeyTitleArray() could not return valid expectation' + ); + } +} diff --git a/test/php/library/Icinga/Web/Dashboard/DashletTest.php b/test/php/library/Icinga/Web/Dashboard/DashletTest.php new file mode 100644 index 000000000..27ab15383 --- /dev/null +++ b/test/php/library/Icinga/Web/Dashboard/DashletTest.php @@ -0,0 +1,12 @@ +dashboard->manageEntry($this->getTestHome()); + $this->dashboard->load(self::TEST_HOME); + + $this->assertCount( + 1, + $this->dashboard->getEntries(), + 'Dashboard::manageEntry() could not manage a new Dashboard Home' + ); + } + + /** + * @depends testWhetherManageEntryManagesANewHomeEntry + */ + public function testWhetherManageEntryUpdatesExistingHomeEntry() + { + $this->dashboard->manageEntry($this->getTestHome()); + $this->dashboard->load(self::TEST_HOME); + + $home = $this->dashboard->getEntry(self::TEST_HOME); + $home->setTitle('Hello'); + + $this->dashboard->manageEntry($home); + $this->dashboard->load(self::TEST_HOME); + + $home = $this->dashboard->getEntry(self::TEST_HOME); + + $this->assertEquals( + 'Hello', + $home->getTitle(), + 'Dashboard::manageEntry() could not update existing Dashboard Home' + ); + } + + /** + * @depends testWhetherManageEntryUpdatesExistingHomeEntry + */ + public function testWhetherRemoveEntryThrowsAnExceptionIfNotExists() + { + $this->expectException(ProgrammingError::class); + + $this->dashboard->removeEntry('test'); + } + + /** + * @depends testWhetherRemoveEntryThrowsAnExceptionIfNotExists + */ + public function testWhetherRemoveEntryRemovesExpectedHomeEntry() + { + $this->dashboard->manageEntry($this->getTestHome('Second Home')); + $this->dashboard->load(); + + $this->dashboard->removeEntry('Second Home'); + $this->dashboard->load(); + + $this->assertFalse( + $this->dashboard->hasEntry('Second Home'), + 'Dashboard::removeEntry() could not remove expected Dashboard Home entry' + ); + } + + /** + * @depends testWhetherRemoveEntryRemovesExpectedHomeEntry + */ + public function testWhetherRemoveEntriesRemovesAllHomeEntries() + { + $this->dashboard->manageEntry($this->getTestHome('Second Home')); + $this->dashboard->load(); + + $this->dashboard->removeEntries(); + $this->dashboard->load(); + + $this->assertTrue( + $this->dashboard->hasEntries(), + 'Dashboard::removeEntries() could not remove all Dashboard Homes' + ); + } + + /** + * @depends testWhetherRemoveEntriesRemovesAllHomeEntries + */ + public function testWhetherLoadHomesLoadsNullHomes() + { + $this->dashboard->load(); + + $this->assertFalse( + $this->dashboard->hasEntries(), + 'Dashboard::load() has loaded Dashboard Homes but should not' + ); + } + + public function testWhetherLoadHomeByNameThrowsAnExceptionIfNotExists() + { + $this->expectException(HttpNotFoundException::class); + + $this->dashboard->load('test'); + } + + public function testWhetherLoadHomesActivatesFirstHome() + { + $this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]); + + $this->dashboard->load(); + + $this->assertEquals( + self::TEST_HOME, + $this->dashboard->getActiveHome()->getName(), + 'Dashboard::load() could not activate expected Dashboard Home' + ); + } + + /** + * @depends testWhetherLoadHomesActivatesFirstHome + */ + public function testWhetherActivateHomeActivatesAHomeEntry() + { + $this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]); + $this->dashboard->load(); + + $active = $this->dashboard->getEntry('Second Home'); + $this->dashboard->activateHome($active); + + $this->assertTrue($active->getActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home'); + } + + /** + * @depends testWhetherActivateHomeActivatesAHomeEntry + */ + public function testWhetherGetActiveHomeGetsExpectedHome() + { + $this->dashboard->addEntry($this->getTestHome()); + $this->dashboard->addEntry($this->getTestHome('Second Home')); + + $active = $this->dashboard->getEntry(self::TEST_HOME); + $this->dashboard->activateHome($active); + + $this->assertEquals( + self::TEST_HOME, + $this->dashboard->getActiveHome()->getName(), + 'Dashboard::getActiveHome() could not return expected Dashboard Home' + ); + } +} diff --git a/test/php/library/Icinga/Web/Dashboard/PaneTest.php b/test/php/library/Icinga/Web/Dashboard/PaneTest.php new file mode 100644 index 000000000..40bc3e2c6 --- /dev/null +++ b/test/php/library/Icinga/Web/Dashboard/PaneTest.php @@ -0,0 +1,113 @@ +activeName ? new Tab(['name' => $this->activeName]) : null; + + return Mockery::mock('ipl\Web\Widget\Tabs') + ->shouldReceive('getActiveTab')->andReturn($activeTab) + ->shouldReceive('activate') + ->getMock(); + } +} + +class PaneTest extends BaseDashboardTestCase +{ + public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermine() + { + $this->expectException(\Icinga\Exception\ConfigurationError::class); + + $home = $this->getTestHome(); + $home->determineActivePane($this->dashboard->getTabs()); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState + */ + public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermineInvalidPane() + { + $this->expectException(ProgrammingError::class); + + Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test'); + + $dashboard = new DashboardWithPredefinableActiveName(); + $home = $this->getTestHome(); + + $home->determineActivePane($dashboard->getTabs()); + } + + /** + * @runInSeparateProcess + * @preserveGlobalState disabled + */ + public function testWhetherDetermineActivePaneDeterminesActiveValidPane() + { + Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test2'); + + $home = $this->getTestHome(); + $home->addEntry(new Pane('test1')); + $home->addEntry(new Pane('test2')); + + $dashboard = new DashboardWithPredefinableActiveName(); + $activePane = $home->determineActivePane($dashboard->getTabs()); + + $this->assertEquals( + 'test2', + $activePane->getName(), + 'DashboardHome::determineActivePane() could not determine valid active pane' + ); + } + + public function testWhetherDetermineActivePaneActivatesTheFirstPane() + { + $home = $this->getTestHome(); + $home->addEntry(new Pane('test1')); + $home->addEntry(new Pane('test2')); + + $this->dashboard->addEntry($home)->activateHome($home); + + $activePane = $home->determineActivePane($this->dashboard->getTabs()); + $this->assertEquals( + 'test1', + $activePane->getName(), + 'DashboardHome::determineActivePane() could not determine/activate the first pane' + ); + } + + public function testWhetherDetermineActivePaneDeterminesActivePane() + { + $dashboard = new DashboardWithPredefinableActiveName(); + $dashboard->activeName = 'test2'; + + $home = $this->getTestHome(); + $home->addEntry(new Pane('test1')); + $home->addEntry(new Pane('test2')); + + $activePane = $home->determineActivePane($dashboard->getTabs()); + $this->assertEquals( + 'test2', + $activePane->getName(), + 'DashboardHome::determineActivePane() could not determine active pane' + ); + } +}