|
4 | 4 |
|
5 | 5 | namespace Tests\Icinga\Web\Dashboard; |
6 | 6 |
|
7 | | -// Necessary as some of these tests disable phpunit's preservation |
8 | | -// of the global state (e.g. autoloaders are in the global state) |
9 | | -require_once realpath(dirname(__FILE__) . '/../../../../bootstrap.php'); |
10 | | - |
11 | 7 | use Icinga\Exception\ProgrammingError; |
12 | 8 | use Icinga\Test\BaseDashboardTestCase; |
13 | | -use Icinga\Web\Dashboard\Dashboard; |
| 9 | +use Icinga\Web\Dashboard\DashboardHome; |
14 | 10 | use Icinga\Web\Dashboard\Pane; |
15 | | -use Icinga\Web\Widget\Tab; |
16 | | -use Mockery; |
17 | 11 |
|
18 | | -class DashboardWithPredefinableActiveName extends Dashboard |
| 12 | +class PaneTest extends BaseDashboardTestCase |
19 | 13 | { |
20 | | - public $activeName = ''; |
| 14 | + const TEST_PANE = 'Test Pane'; |
21 | 15 |
|
22 | | - public function getTabs() |
| 16 | + protected function getTestPane(string $name = self::TEST_PANE): Pane |
23 | 17 | { |
24 | | - $activeTab = $this->activeName ? new Tab(['name' => $this->activeName]) : null; |
25 | | - |
26 | | - return Mockery::mock('ipl\Web\Widget\Tabs') |
27 | | - ->shouldReceive('getActiveTab')->andReturn($activeTab) |
28 | | - ->shouldReceive('activate') |
29 | | - ->getMock(); |
| 18 | + return new Pane($name); |
30 | 19 | } |
31 | | -} |
32 | 20 |
|
33 | | -class PaneTest extends BaseDashboardTestCase |
34 | | -{ |
35 | | - public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermine() |
| 21 | + public function testWhetherActivatePaneThrowsAnExceptionIfNotExists() |
36 | 22 | { |
37 | | - $this->expectException(\Icinga\Exception\ConfigurationError::class); |
| 23 | + $this->expectException(ProgrammingError::class); |
38 | 24 |
|
39 | 25 | $home = $this->getTestHome(); |
40 | | - $home->determineActivePane($this->dashboard->getTabs()); |
| 26 | + $home->activatePane(new Pane(self::TEST_PANE)); |
| 27 | + } |
| 28 | + |
| 29 | + public function testWhetherActivatePaneActivatesExpectedPane() |
| 30 | + { |
| 31 | + $home = $this->getTestHome(); |
| 32 | + $home->addEntry($this->getTestPane()); |
| 33 | + |
| 34 | + $home->activatePane($home->getEntry(self::TEST_PANE)); |
| 35 | + |
| 36 | + $this->assertEquals( |
| 37 | + self::TEST_PANE, |
| 38 | + $home->getActivePane()->getName(), |
| 39 | + 'DashboardHome::activatePane() could not activate expected Dashboard Pane' |
| 40 | + ); |
41 | 41 | } |
42 | 42 |
|
43 | 43 | /** |
44 | | - * @runInSeparateProcess |
45 | | - * @preserveGlobalState |
| 44 | + * @depends testWhetherActivatePaneActivatesExpectedPane |
46 | 45 | */ |
47 | | - public function testWhetherDetermineActivePaneThrowsAnExceptionIfCouldNotDetermineInvalidPane() |
| 46 | + public function testWhetherLoadDashboardEntriesActivatesFirstPane() |
48 | 47 | { |
49 | | - $this->expectException(ProgrammingError::class); |
| 48 | + $home = $this->getTestHome(); |
| 49 | + $this->dashboard->manageEntry($home); |
50 | 50 |
|
51 | | - Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test'); |
| 51 | + $home->manageEntry($this->getTestPane()); |
| 52 | + $home->manageEntry($this->getTestPane('Test Me')); |
52 | 53 |
|
53 | | - $dashboard = new DashboardWithPredefinableActiveName(); |
54 | | - $home = $this->getTestHome(); |
| 54 | + $this->dashboard->load(); |
| 55 | + $home = $this->dashboard->getActiveHome(); |
55 | 56 |
|
56 | | - $home->determineActivePane($dashboard->getTabs()); |
| 57 | + $this->assertEquals( |
| 58 | + self::TEST_PANE, |
| 59 | + $home->getActivePane()->getName(), |
| 60 | + 'DashboardHome::loadDashboardEntries() could not activate expected Dashboard Pane' |
| 61 | + ); |
57 | 62 | } |
58 | 63 |
|
59 | 64 | /** |
60 | | - * @runInSeparateProcess |
61 | | - * @preserveGlobalState disabled |
| 65 | + * @depends testWhetherLoadDashboardEntriesActivatesFirstPane |
62 | 66 | */ |
63 | | - public function testWhetherDetermineActivePaneDeterminesActiveValidPane() |
| 67 | + public function testWhetherActivatePaneActivatesAPaneEntry() |
64 | 68 | { |
65 | | - Mockery::mock('alias:ipl\Web\Url')->shouldReceive('fromRequest->getParam')->andReturn('test2'); |
66 | | - |
67 | 69 | $home = $this->getTestHome(); |
68 | | - $home->addEntry(new Pane('test1')); |
69 | | - $home->addEntry(new Pane('test2')); |
| 70 | + $this->dashboard->manageEntry($home); |
| 71 | + |
| 72 | + $home->manageEntry(new Pane(self::TEST_PANE)); |
70 | 73 |
|
71 | | - $dashboard = new DashboardWithPredefinableActiveName(); |
72 | | - $activePane = $home->determineActivePane($dashboard->getTabs()); |
| 74 | + $this->dashboard->load(self::TEST_HOME, self::TEST_PANE); |
| 75 | + $home = $this->dashboard->getActiveHome(); |
73 | 76 |
|
74 | 77 | $this->assertEquals( |
75 | | - 'test2', |
76 | | - $activePane->getName(), |
77 | | - 'DashboardHome::determineActivePane() could not determine valid active pane' |
| 78 | + self::TEST_PANE, |
| 79 | + $home->getActivePane()->getName(), |
| 80 | + 'DashboardHome::loadDashboardEntries() could not load and activate expected Dashboard Pane' |
78 | 81 | ); |
79 | 82 | } |
80 | 83 |
|
81 | | - public function testWhetherDetermineActivePaneActivatesTheFirstPane() |
| 84 | + /** |
| 85 | + * @depends testWhetherActivatePaneActivatesAPaneEntry |
| 86 | + */ |
| 87 | + public function testWhetherGetActivePaneGetsExpectedPane() |
82 | 88 | { |
83 | 89 | $home = $this->getTestHome(); |
84 | | - $home->addEntry(new Pane('test1')); |
85 | | - $home->addEntry(new Pane('test2')); |
| 90 | + $home->addEntry($this->getTestPane()); |
| 91 | + $home->addEntry($this->getTestPane('Test Me')); |
86 | 92 |
|
87 | | - $this->dashboard->addEntry($home)->activateHome($home); |
| 93 | + $home->activatePane($home->getEntry('Test Me')); |
88 | 94 |
|
89 | | - $activePane = $home->determineActivePane($this->dashboard->getTabs()); |
90 | 95 | $this->assertEquals( |
91 | | - 'test1', |
92 | | - $activePane->getName(), |
93 | | - 'DashboardHome::determineActivePane() could not determine/activate the first pane' |
| 96 | + 'Test Me', |
| 97 | + $home->getActivePane()->getName(), |
| 98 | + 'DashboardHome::getActivePane() could not determine valid active pane' |
94 | 99 | ); |
95 | 100 | } |
96 | 101 |
|
97 | | - public function testWhetherDetermineActivePaneDeterminesActivePane() |
| 102 | + /** |
| 103 | + * @depends testWhetherActivatePaneActivatesAPaneEntry |
| 104 | + */ |
| 105 | + public function testWhetherManageEntryManagesANewPaneEntry() |
98 | 106 | { |
99 | | - $dashboard = new DashboardWithPredefinableActiveName(); |
100 | | - $dashboard->activeName = 'test2'; |
| 107 | + $home = $this->getTestHome(); |
| 108 | + $this->dashboard->manageEntry($home); |
| 109 | + |
| 110 | + $home->manageEntry($this->getTestPane()); |
101 | 111 |
|
| 112 | + $this->dashboard->load(self::TEST_HOME); |
| 113 | + $home = $this->dashboard->getActiveHome(); |
| 114 | + |
| 115 | + $this->assertCount( |
| 116 | + 1, |
| 117 | + $home->getEntries(), |
| 118 | + 'DashboardHome::manageEntry() could not manage a new Dashboard Pane' |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @depends testWhetherManageEntryManagesANewPaneEntry |
| 124 | + */ |
| 125 | + public function testWhetherManageEntryUpdatesExistingPaneEntry() |
| 126 | + { |
102 | 127 | $home = $this->getTestHome(); |
103 | | - $home->addEntry(new Pane('test1')); |
104 | | - $home->addEntry(new Pane('test2')); |
| 128 | + $this->dashboard->manageEntry($home); |
| 129 | + |
| 130 | + $home->manageEntry($this->getTestPane()); |
| 131 | + |
| 132 | + $this->dashboard->load(self::TEST_HOME); |
| 133 | + |
| 134 | + $home = $this->dashboard->getActiveHome(); |
| 135 | + $home->getActivePane()->setTitle('Hello'); |
| 136 | + |
| 137 | + $home->manageEntry($home->getEntries()); |
| 138 | + $this->dashboard->load(self::TEST_HOME); |
| 139 | + |
| 140 | + $home = $this->dashboard->getActiveHome(); |
105 | 141 |
|
106 | | - $activePane = $home->determineActivePane($dashboard->getTabs()); |
107 | 142 | $this->assertEquals( |
108 | | - 'test2', |
109 | | - $activePane->getName(), |
110 | | - 'DashboardHome::determineActivePane() could not determine active pane' |
| 143 | + 'Hello', |
| 144 | + $home->getActivePane()->getTitle(), |
| 145 | + 'DashboardHome::manageEntry() could not update existing Dashboard Pane' |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @depends testWhetherManageEntryUpdatesExistingPaneEntry |
| 151 | + */ |
| 152 | + public function testWhetherManageEntryMovesAPaneToAnotherExistingHome() |
| 153 | + { |
| 154 | + $home = $this->getTestHome('Second Home'); |
| 155 | + $this->dashboard->manageEntry([$this->getTestHome(), $home]); |
| 156 | + |
| 157 | + $home->manageEntry([$this->getTestPane(), $this->getTestPane('Test Me')]); |
| 158 | + |
| 159 | + $this->dashboard->load('Second Home', null, true); |
| 160 | + |
| 161 | + $home = $this->dashboard->getActiveHome(); |
| 162 | + /** @var DashboardHome $default */ |
| 163 | + $default = $this->dashboard->getEntry(self::TEST_HOME); |
| 164 | + |
| 165 | + $default->manageEntry($home->getEntry(self::TEST_PANE), $home); |
| 166 | + $this->dashboard->load(self::TEST_HOME); |
| 167 | + |
| 168 | + $default = $this->dashboard->getActiveHome(); |
| 169 | + |
| 170 | + $this->assertCount( |
| 171 | + 1, |
| 172 | + $default->getEntries(), |
| 173 | + 'DashboardHome::manageEntry() could not move a Dashboard Pane to another existing Dashboard Home' |
111 | 174 | ); |
112 | 175 | } |
113 | 176 | } |
0 commit comments