|
25 | 25 | */ |
26 | 26 | use Behat\Behat\Context\Context; |
27 | 27 | use Behat\Gherkin\Node\TableNode; |
| 28 | +use GuzzleHttp\Client; |
28 | 29 | use PHPUnit\Framework\Assert; |
29 | 30 |
|
30 | 31 | require __DIR__ . '/../../vendor/autoload.php'; |
@@ -70,4 +71,94 @@ protected function resetAppConfigs(): void { |
70 | 71 | $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match'); |
71 | 72 | $this->deleteServerConfig('core', 'shareapi_only_share_with_group_members'); |
72 | 73 | } |
| 74 | + |
| 75 | + /** |
| 76 | + * @Given /^user "([^"]*)" has status "([^"]*)"$/ |
| 77 | + * @param string $user |
| 78 | + * @param string $status |
| 79 | + */ |
| 80 | + public function assureUserHasStatus($user, $status) { |
| 81 | + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status/status"; |
| 82 | + $client = new Client(); |
| 83 | + $options = [ |
| 84 | + 'headers' => [ |
| 85 | + 'OCS-APIREQUEST' => 'true', |
| 86 | + ], |
| 87 | + ]; |
| 88 | + if ($user === 'admin') { |
| 89 | + $options['auth'] = $this->adminUser; |
| 90 | + } else { |
| 91 | + $options['auth'] = [$user, $this->regularUser]; |
| 92 | + } |
| 93 | + |
| 94 | + $options['form_params'] = [ |
| 95 | + 'statusType' => $status |
| 96 | + ]; |
| 97 | + |
| 98 | + $this->response = $client->put($fullUrl, $options); |
| 99 | + $this->theHTTPStatusCodeShouldBe(200); |
| 100 | + |
| 101 | + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status"; |
| 102 | + unset($options['form_params']); |
| 103 | + $this->response = $client->get($fullUrl, $options); |
| 104 | + $this->theHTTPStatusCodeShouldBe(200); |
| 105 | + |
| 106 | + $returnedStatus = json_decode(json_encode(simplexml_load_string($this->response->getBody()->getContents())->data), true)['status']; |
| 107 | + Assert::assertEquals($status, $returnedStatus); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @param string $user |
| 112 | + * @return null|array |
| 113 | + */ |
| 114 | + public function getStatusList(string $user): ?array { |
| 115 | + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/statuses"; |
| 116 | + $client = new Client(); |
| 117 | + $options = [ |
| 118 | + 'headers' => [ |
| 119 | + 'OCS-APIREQUEST' => 'true', |
| 120 | + ], |
| 121 | + ]; |
| 122 | + if ($user === 'admin') { |
| 123 | + $options['auth'] = $this->adminUser; |
| 124 | + } else { |
| 125 | + $options['auth'] = [$user, $this->regularUser]; |
| 126 | + } |
| 127 | + |
| 128 | + $this->response = $client->get($fullUrl, $options); |
| 129 | + $this->theHTTPStatusCodeShouldBe(200); |
| 130 | + |
| 131 | + $contents = $this->response->getBody()->getContents(); |
| 132 | + return json_decode(json_encode(simplexml_load_string($contents)->data), true); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * @Given /^user statuses for "([^"]*)" list "([^"]*)" with status "([^"]*)"$/ |
| 137 | + * @param string $user |
| 138 | + * @param string $statusUser |
| 139 | + * @param string $status |
| 140 | + */ |
| 141 | + public function assertStatusesList(string $user, string $statusUser, string $status): void { |
| 142 | + $statusList = $this->getStatusList($user); |
| 143 | + Assert::assertArrayHasKey('element', $statusList, 'Returned status list empty or broken'); |
| 144 | + if (array_key_exists('userId', $statusList['element'])) { |
| 145 | + // If only one user has a status set, the API returns their status directly |
| 146 | + Assert::assertArrayHasKey('status', $statusList['element'], 'Returned status list empty or broken'); |
| 147 | + $filteredStatusList = [ $statusList['element']['userId'] => $statusList['element']['status'] ]; |
| 148 | + } else { |
| 149 | + // If more than one user have their status set, the API returns an array of their statuses |
| 150 | + $filteredStatusList = array_column($statusList['element'], 'status', 'userId'); |
| 151 | + } |
| 152 | + Assert::assertArrayHasKey($statusUser, $filteredStatusList, 'User not listed in statuses: ' . $statusUser); |
| 153 | + Assert::assertEquals($status, $filteredStatusList[$statusUser]); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * @Given /^user statuses for "([^"]*)" are empty$/ |
| 158 | + * @param string $user |
| 159 | + */ |
| 160 | + public function assertStatusesEmpty(string $user): void { |
| 161 | + $statusList = $this->getStatusList($user); |
| 162 | + Assert::assertEmpty($statusList); |
| 163 | + } |
73 | 164 | } |
0 commit comments