|
| 1 | +<?php |
| 2 | + |
| 3 | +/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */ |
| 4 | + |
| 5 | +namespace Tests\Icinga\Web\Dashboard; |
| 6 | + |
| 7 | +use Icinga\Exception\Http\HttpNotFoundException; |
| 8 | +use Icinga\Exception\ProgrammingError; |
| 9 | +use Icinga\Test\BaseDashboardTestCase; |
| 10 | + |
| 11 | +class HomeTest extends BaseDashboardTestCase |
| 12 | +{ |
| 13 | + public function testWhetherManageEntryManagesANewHomeEntry() |
| 14 | + { |
| 15 | + $this->dashboard->manageEntry($this->getTestHome()); |
| 16 | + $this->dashboard->load(self::TEST_HOME); |
| 17 | + |
| 18 | + $this->assertCount( |
| 19 | + 1, |
| 20 | + $this->dashboard->getEntries(), |
| 21 | + 'Dashboard::manageEntry() could not manage a new Dashboard Home' |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @depends testWhetherManageEntryManagesANewHomeEntry |
| 27 | + */ |
| 28 | + public function testWhetherManageEntryUpdatesExistingHomeEntry() |
| 29 | + { |
| 30 | + $this->dashboard->manageEntry($this->getTestHome()); |
| 31 | + $this->dashboard->load(self::TEST_HOME); |
| 32 | + |
| 33 | + $home = $this->dashboard->getEntry(self::TEST_HOME); |
| 34 | + $home->setTitle('Hello'); |
| 35 | + |
| 36 | + $this->dashboard->manageEntry($home); |
| 37 | + $this->dashboard->load(self::TEST_HOME); |
| 38 | + |
| 39 | + $home = $this->dashboard->getEntry(self::TEST_HOME); |
| 40 | + |
| 41 | + $this->assertEquals( |
| 42 | + 'Hello', |
| 43 | + $home->getTitle(), |
| 44 | + 'Dashboard::manageEntry() could not update existing Dashboard Home' |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @depends testWhetherManageEntryUpdatesExistingHomeEntry |
| 50 | + */ |
| 51 | + public function testWhetherRemoveEntryThrowsAnExceptionIfNotExists() |
| 52 | + { |
| 53 | + $this->expectException(ProgrammingError::class); |
| 54 | + |
| 55 | + $this->dashboard->removeEntry('test'); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @depends testWhetherRemoveEntryThrowsAnExceptionIfNotExists |
| 60 | + */ |
| 61 | + public function testWhetherRemoveEntryRemovesExpectedHomeEntry() |
| 62 | + { |
| 63 | + $this->dashboard->manageEntry($this->getTestHome('Second Home')); |
| 64 | + $this->dashboard->load(); |
| 65 | + |
| 66 | + $this->dashboard->removeEntry('Second Home'); |
| 67 | + $this->dashboard->load(); |
| 68 | + |
| 69 | + $this->assertFalse( |
| 70 | + $this->dashboard->hasEntry('Second Home'), |
| 71 | + 'Dashboard::removeEntry() could not remove expected Dashboard Home entry' |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @depends testWhetherRemoveEntryRemovesExpectedHomeEntry |
| 77 | + */ |
| 78 | + public function testWhetherRemoveEntriesRemovesAllHomeEntries() |
| 79 | + { |
| 80 | + $this->dashboard->manageEntry($this->getTestHome('Second Home')); |
| 81 | + $this->dashboard->load(); |
| 82 | + |
| 83 | + $this->dashboard->removeEntries(); |
| 84 | + $this->dashboard->load(); |
| 85 | + |
| 86 | + $this->assertTrue( |
| 87 | + $this->dashboard->hasEntries(), |
| 88 | + 'Dashboard::removeEntries() could not remove all Dashboard Homes' |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * @depends testWhetherRemoveEntriesRemovesAllHomeEntries |
| 94 | + */ |
| 95 | + public function testWhetherLoadHomesLoadsNullHomes() |
| 96 | + { |
| 97 | + $this->dashboard->load(); |
| 98 | + |
| 99 | + $this->assertFalse( |
| 100 | + $this->dashboard->hasEntries(), |
| 101 | + 'Dashboard::load() has loaded Dashboard Homes but should not' |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + public function testWhetherLoadHomeByNameThrowsAnExceptionIfNotExists() |
| 106 | + { |
| 107 | + $this->expectException(HttpNotFoundException::class); |
| 108 | + |
| 109 | + $this->dashboard->load('test'); |
| 110 | + } |
| 111 | + |
| 112 | + public function testWhetherLoadHomesActivatesFirstHome() |
| 113 | + { |
| 114 | + $this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]); |
| 115 | + |
| 116 | + $this->dashboard->load(); |
| 117 | + |
| 118 | + $this->assertEquals( |
| 119 | + self::TEST_HOME, |
| 120 | + $this->dashboard->getActiveHome()->getName(), |
| 121 | + 'Dashboard::load() could not activate expected Dashboard Home' |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @depends testWhetherLoadHomesActivatesFirstHome |
| 127 | + */ |
| 128 | + public function testWhetherActivateHomeActivatesAHomeEntry() |
| 129 | + { |
| 130 | + $this->dashboard->manageEntry([$this->getTestHome(), $this->getTestHome('Second Home')]); |
| 131 | + $this->dashboard->load(); |
| 132 | + |
| 133 | + $active = $this->dashboard->getEntry('Second Home'); |
| 134 | + $this->dashboard->activateHome($active); |
| 135 | + |
| 136 | + $this->assertTrue($active->getActive(), 'Dashboard::activateHome() could not activate expected Dashboard Home'); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * @depends testWhetherActivateHomeActivatesAHomeEntry |
| 141 | + */ |
| 142 | + public function testWhetherGetActiveHomeGetsExpectedHome() |
| 143 | + { |
| 144 | + $this->dashboard->addEntry($this->getTestHome()); |
| 145 | + $this->dashboard->addEntry($this->getTestHome('Second Home')); |
| 146 | + |
| 147 | + $active = $this->dashboard->getEntry(self::TEST_HOME); |
| 148 | + $this->dashboard->activateHome($active); |
| 149 | + |
| 150 | + $this->assertEquals( |
| 151 | + self::TEST_HOME, |
| 152 | + $this->dashboard->getActiveHome()->getName(), |
| 153 | + 'Dashboard::getActiveHome() could not return expected Dashboard Home' |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments