EGroupware /
egroupware
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Tests for edit group command |
||
| 5 | * |
||
| 6 | * It would be good to check to see if the hooks get called, but that's impossible |
||
| 7 | * with static hook calls. |
||
| 8 | * |
||
| 9 | * @link http://www.egroupware.org |
||
| 10 | * @author Nathan Gray |
||
| 11 | * @copyright (c) 2018 Nathan Gray |
||
| 12 | * @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License |
||
| 13 | */ |
||
| 14 | |||
| 15 | // test base providing common stuff |
||
| 16 | require_once __DIR__.'/CommandBase.php'; |
||
| 17 | |||
| 18 | use EGroupware\Api; |
||
| 19 | |||
| 20 | class GroupCommandTest extends CommandBase { |
||
| 21 | |||
| 22 | // User for testing |
||
| 23 | protected $account_id; |
||
| 24 | |||
| 25 | // Define group details once, then modify as needed for tests |
||
| 26 | protected $group = array( |
||
| 27 | 'account_lid' => 'Group Command Test', |
||
| 28 | 'account_members' => array() |
||
| 29 | ); |
||
| 30 | |||
| 31 | protected function setUp() : void |
||
| 32 | { |
||
| 33 | // Can't set this until now - value is not available |
||
| 34 | $this->group['account_members'] = array($GLOBALS['egw_info']['user']['account_id']); |
||
| 35 | |||
| 36 | if(($account_id = $GLOBALS['egw']->accounts->name2id($this->group['account_lid']))) |
||
| 37 | { |
||
| 38 | // Delete if there in case something went wrong |
||
| 39 | $GLOBALS['egw']->accounts->delete($account_id); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | protected function tearDown() : void |
||
| 43 | { |
||
| 44 | // Delete the accounts we created |
||
| 45 | if($this->group_id) |
||
| 46 | { |
||
| 47 | $GLOBALS['egw']->accounts->delete($this->group_id); |
||
| 48 | } |
||
| 49 | if($this->account_id) |
||
| 50 | { |
||
| 51 | $GLOBALS['egw']->accounts->delete($this->account_id); |
||
| 52 | } |
||
| 53 | parent::tearDown(); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Test that adding a group works when we give it what it needs |
||
| 58 | */ |
||
| 59 | public function testAddGroup() |
||
| 60 | { |
||
| 61 | // Set up |
||
| 62 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 63 | $log_count = $this->get_log_count(); |
||
| 64 | |||
| 65 | // Execute |
||
| 66 | $command = new admin_cmd_edit_group(false, $this->group); |
||
| 67 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 68 | $command->run(); |
||
| 69 | $this->group_id = $command->account; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 70 | |||
| 71 | // Check |
||
| 72 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 73 | |||
| 74 | $this->assertNotEmpty($this->group_id, 'Did not create test group account'); |
||
| 75 | $this->assertEquals(count($pre_search) + 1, count($post_search), 'Should have one more account than before'); |
||
| 76 | $this->assertArrayHasKey($this->group_id, $post_search); |
||
| 77 | $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Try to add a new group with the same name as existing group. It should |
||
| 82 | * throw an exception |
||
| 83 | */ |
||
| 84 | public function testGroupAlreadyExists() |
||
| 85 | { |
||
| 86 | // Set up |
||
| 87 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 88 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
| 89 | |||
| 90 | // Execute |
||
| 91 | $this->account['account_lid'] = 'Default'; |
||
|
0 ignored issues
–
show
|
|||
| 92 | $command = new admin_cmd_edit_group(false, $this->account); |
||
| 93 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 94 | $command->run(); |
||
| 95 | $this->group_id = $command->account; |
||
|
0 ignored issues
–
show
|
|||
| 96 | |||
| 97 | // Check |
||
| 98 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 99 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Try to add a new group without specifying the name. It should throw an |
||
| 104 | * exception |
||
| 105 | */ |
||
| 106 | public function testNameMissing() |
||
| 107 | { |
||
| 108 | // Set up |
||
| 109 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 110 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
| 111 | $account = $this->group; |
||
| 112 | unset($account['account_lid']); |
||
| 113 | |||
| 114 | // Execute |
||
| 115 | $command = new admin_cmd_edit_group(false, $account); |
||
| 116 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 117 | $command->run(); |
||
| 118 | $this->group_id = $command->account; |
||
|
0 ignored issues
–
show
|
|||
| 119 | |||
| 120 | // Check |
||
| 121 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 122 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Try to add a new group without specifying members. It should throw |
||
| 127 | * an exception |
||
| 128 | */ |
||
| 129 | public function testMembersMissing() |
||
| 130 | { |
||
| 131 | // Set up |
||
| 132 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 133 | $this->expectException(Api\Exception\WrongUserinput::class); |
||
| 134 | $account = $this->group; |
||
| 135 | unset($account['account_members']); |
||
| 136 | |||
| 137 | // Execute |
||
| 138 | $command = new admin_cmd_edit_group(false, $account); |
||
| 139 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 140 | $command->run(); |
||
| 141 | $this->group_id = $command->account; |
||
|
0 ignored issues
–
show
|
|||
| 142 | |||
| 143 | // Check |
||
| 144 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 145 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Test adding & removing a new member |
||
| 150 | * |
||
| 151 | * @depends UserCommandTest::testAddUser |
||
| 152 | */ |
||
| 153 | public function testChangeMembers() |
||
| 154 | { |
||
| 155 | // Set up |
||
| 156 | // Make a new user so it doesn't matter if something goes wrong |
||
| 157 | $account = array( |
||
| 158 | 'account_lid' => 'test_user', |
||
| 159 | 'account_firstname' => 'TestUser', |
||
| 160 | 'account_lastname' => 'Test', |
||
| 161 | 'account_primary_group' => 'Default', |
||
| 162 | 'account_groups' => array('Default') |
||
| 163 | ); |
||
| 164 | |||
| 165 | if(($account_id = $GLOBALS['egw']->accounts->name2id($account['account_lid']))) |
||
| 166 | { |
||
| 167 | // Delete if there in case something went wrong |
||
| 168 | $GLOBALS['egw']->accounts->delete($account_id); |
||
| 169 | } |
||
| 170 | |||
| 171 | $command = new admin_cmd_edit_user(false, $account); |
||
| 172 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 173 | $command->run(); |
||
| 174 | $this->account_id = $command->account; |
||
| 175 | |||
| 176 | $command = new admin_cmd_edit_group(false, $this->group); |
||
| 177 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 178 | $command->run(); |
||
| 179 | $this->group_id = $command->account; |
||
|
0 ignored issues
–
show
|
|||
| 180 | |||
| 181 | // Count accounts |
||
| 182 | $pre_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 183 | $log_count = $this->get_log_count(); |
||
| 184 | |||
| 185 | // Execute |
||
| 186 | $account = $this->account; |
||
| 187 | $account['account_members'][] = $this->account_id; |
||
| 188 | $command = new admin_cmd_edit_group($this->group_id, $account); |
||
| 189 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 190 | $command->run(); |
||
| 191 | |||
| 192 | // Check |
||
| 193 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 194 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
| 195 | $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); |
||
| 196 | |||
| 197 | // Now remove |
||
| 198 | $pre_search = $post_search; |
||
| 199 | $log_count = $this->get_log_count(); |
||
| 200 | |||
| 201 | $account = $this->account; |
||
| 202 | $command = new admin_cmd_edit_group($this->group_id, $account); |
||
| 203 | $command->comment = 'Needed for unit test ' . $this->getName(); |
||
| 204 | $command->run(); |
||
| 205 | |||
| 206 | // Check |
||
| 207 | $post_search = $GLOBALS['egw']->accounts->search(array('type' => 'both')); |
||
| 208 | $this->assertEquals(count($pre_search), count($post_search), 'Should have same number of accounts as before'); |
||
| 209 | $this->assertGreaterThan($log_count, $this->get_log_count(), "Command ($command) did not log"); |
||
| 210 | } |
||
| 211 | } |