From db9b4c611f641df7d5bef51016152776c5b9b0af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Km=C3=ADnek?= Date: Fri, 27 Sep 2024 22:20:22 +0200 Subject: [PATCH] feat: Separate tests underneath two main parents - ApiTest / UITest to be able to use shared recordmanager --- .../app/attendance/AttendanceStatusTest.php | 8 +- .../app/attendance/AttendanceTest.php | 12 +-- .../autotest/app/authentication/IsTest.php | 6 +- app/module/autotest/app/debt/DebtTest.php | 15 ++- .../discussion/DiscussionPresenterTest.phpt | 94 +++++++++++++++++++ .../app/discussion/DiscussionTest.php | 19 ++-- app/module/autotest/app/event/EventTest.php | 6 +- .../app/multiaccount/MultiaccountTest.php | 4 +- .../app/permission/PermissionTest.php | 8 +- app/module/autotest/app/poll/PollTest.php | 8 +- app/module/autotest/app/user/UserTest.php | 7 +- app/module/autotest/entity/ApiTest.php | 48 ++++++++++ app/module/autotest/entity/RequestCase.php | 44 +-------- app/module/autotest/entity/UITest.php | 23 +++++ app/module/autotest/manager/RecordManager.php | 61 +++++++----- 15 files changed, 247 insertions(+), 116 deletions(-) create mode 100644 app/module/autotest/app/discussion/DiscussionPresenterTest.phpt create mode 100644 app/module/autotest/entity/ApiTest.php create mode 100644 app/module/autotest/entity/UITest.php diff --git a/app/module/autotest/app/attendance/AttendanceStatusTest.php b/app/module/autotest/app/attendance/AttendanceStatusTest.php index 2eb38938..212f19c6 100644 --- a/app/module/autotest/app/attendance/AttendanceStatusTest.php +++ b/app/module/autotest/app/attendance/AttendanceStatusTest.php @@ -6,8 +6,10 @@ use Tymy\Bootstrap; use Tymy\Module\Attendance\Model\Attendance; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; + +use function count; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -19,7 +21,7 @@ * @RequestMapping(value = "/attendanceStatusSet/{id}", method = RequestMethod.PUT) * @RequestMapping(value = "/attendanceStatusSet/{id}", method = RequestMethod.DELETE) */ -class AttendanceStatusTest extends RequestCase +class AttendanceStatusTest extends ApiTest { protected function getBasePath(): string { @@ -174,7 +176,7 @@ private function getStatusSetId(): int return $allStatusSets[random_int(0, (is_countable($allStatusSets) ? count($allStatusSets) : 0) - 1)]["id"]; } - public function createRecord(): void + public function createRecord(): array { //use creator from recorManager - can create status or statusSet } diff --git a/app/module/autotest/app/attendance/AttendanceTest.php b/app/module/autotest/app/attendance/AttendanceTest.php index f70b8e3b..ce828bc1 100644 --- a/app/module/autotest/app/attendance/AttendanceTest.php +++ b/app/module/autotest/app/attendance/AttendanceTest.php @@ -6,8 +6,8 @@ use Tymy\Bootstrap; use Tymy\Module\Attendance\Model\Attendance; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -15,7 +15,7 @@ /** * Description of AttendanceTest */ -class AttendanceTest extends RequestCase +class AttendanceTest extends ApiTest { private ?int $eventId = null; @@ -28,7 +28,7 @@ public function testPost(): void { $this->authorizeAdmin(); //pre - $this->eventId = $this->recordManager->createEvent(); + $this->eventId = $this->recordManager->createEvent()["id"]; $mocked = $this->mockRecord(); $this->authorizeUser(); @@ -102,7 +102,7 @@ public function testPost(): void public function testViewPlanForbidden(): void { $this->authorizeAdmin(); - $eventId = $this->recordManager->createEvent(null, ["resultRightName" => null]); + $eventId = $this->recordManager->createEvent(null, ["resultRightName" => null])["id"]; $this->authorizeUser(); @@ -123,7 +123,7 @@ public function testViewPlanForbidden(): void public function testPermissivePost(): void { $this->authorizeAdmin(); - $this->eventId = $this->recordManager->createEvent(null, ["viewRightName" => "ADMINONLY", "planRightName" => "ADMINONLY", "resultRightName" => "ADMINMEMBER"]); + $this->eventId = $this->recordManager->createEvent(null, ["viewRightName" => "ADMINONLY", "planRightName" => "ADMINONLY", "resultRightName" => "ADMINMEMBER"])["id"]; $mocked = $this->mockRecord(); $this->authorizeUser(); @@ -153,7 +153,7 @@ public function testUnknownEvent(): void $this->request($this->getBasePath(), "POST", $mocked)->expect(404); //event doesnt exist } - public function createRecord(): void + public function createRecord(): array { //attendance are never created solely, always just posted to some event } diff --git a/app/module/autotest/app/authentication/IsTest.php b/app/module/autotest/app/authentication/IsTest.php index 5bdf87a1..60dc1cc4 100644 --- a/app/module/autotest/app/authentication/IsTest.php +++ b/app/module/autotest/app/authentication/IsTest.php @@ -5,7 +5,7 @@ namespace Tymy\Module\Autotest\Authentication; use Tymy\Bootstrap; -use Tymy\Module\Autotest\RequestCase; +use Tymy\Module\Autotest\ApiTest; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -13,7 +13,7 @@ /** * Description of IsTest */ -class IsTest extends RequestCase +class IsTest extends ApiTest { public function testUnauthorized(): void { @@ -33,7 +33,7 @@ protected function mockChanges(): array return []; } - public function createRecord(): void + public function createRecord(): array { //not used in this test } diff --git a/app/module/autotest/app/debt/DebtTest.php b/app/module/autotest/app/debt/DebtTest.php index 3815c8fa..de4d965f 100644 --- a/app/module/autotest/app/debt/DebtTest.php +++ b/app/module/autotest/app/debt/DebtTest.php @@ -6,9 +6,8 @@ use Nette\Utils\DateTime; use Tymy\Bootstrap; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; -use Tymy\Module\Core\Model\BaseModel; use Tymy\Module\Debt\Model\Debt; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; @@ -17,7 +16,7 @@ /** * Description of DebtTest */ -class DebtTest extends RequestCase +class DebtTest extends ApiTest { public function getModule(): string { @@ -70,8 +69,8 @@ public function testTeamDebts(): void { //admin can create team debt $this->authorizeAdmin(); - $recordId = $this->recordManager->createDebt(null, ["payeeId" => null, "caption" => "Poplatky 2020", "note" => null]); //create debt for team - $origData = $this->request($this->getBasePath() . "/" . $recordId)->expect(200, "array"); + $origData = $this->recordManager->createDebt(null, ["payeeId" => null, "caption" => "Poplatky 2020", "note" => null]); //create debt for team + $recordId = $origData["id"]; //another admin of team debts can see that debt $this->authorizeAdmin($this->config["user_member_login"], $this->config["user_member_pwd"]); @@ -105,7 +104,7 @@ public function testTeamDebts(): void Assert::true($found, "Debt id $recordId not found in users list"); $chResponse = $this->request($this->getBasePath() . "/" . $recordId, "PUT", $this->mockChanges())->expect(200, "array");//debtor can edit, but the only field that gets edited is paymentSent - Assert::equal($origData->getData()["amount"], $chResponse->getData()["amount"]);//amount didnt change + Assert::equal($origData["amount"], $chResponse->getData()["amount"]);//amount didnt change $now = new DateTime(); $this->request($this->getBasePath() . "/" . $recordId, "PUT", ["paymentSent" => $this->toJsonDate($now)])->expect(200, "array"); sleep(1);//sleep for one second, to make sure that current datetime is now different than $now variable. So we can check that the paymentSent would be actually changed if something changes it @@ -159,7 +158,7 @@ public function testTeamOwesMe(): void "payeeId" => $this->config["user_test_id"], "debtorId" => 0, "caption" => "Tým mi dluží přeplatek za finále MČR, id: " . $this->user->getId() - ]); + ])["id"]; //admin can mark it as paymentSent $this->authorizeAdmin($this->config["user_member_login"], $this->config["user_member_pwd"]); @@ -183,7 +182,7 @@ public function testCRUDPlural(): void $this->deleteRecord($recordId); } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createDebt(); } diff --git a/app/module/autotest/app/discussion/DiscussionPresenterTest.phpt b/app/module/autotest/app/discussion/DiscussionPresenterTest.phpt new file mode 100644 index 00000000..b11b1e58 --- /dev/null +++ b/app/module/autotest/app/discussion/DiscussionPresenterTest.phpt @@ -0,0 +1,94 @@ +authorizeUser(); + $request = new Nette\Application\Request($this->getPresenterName(), 'GET', array('action' => 'default')); + $response = $this->presenter->run($request); + + Assert::type('Nette\Application\Responses\TextResponse', $response); + + $html = (string) $response->getSource(); + $dom = DomQuery::fromHtml($html); + + //has navbar + Assert::true($dom->has('div#snippet-navbar-nav')); + //has breadcrumbs + Assert::true($dom->has('div.container')); + Assert::true($dom->has('ol.breadcrumb')); + Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item a[href]')), 1); + Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item')), 2); //last item aint link + + Assert::true($dom->has('div.container.discussions')); + Assert::true(count($dom->find('div.container.discussions div.row')) >= 1); + } + + function testActionDiscussion($discussionName, $canWrite) + { + $this->authorizeAdmin(); + $discussion = $this->recordManager->createDiscussion(); + $discussionName = $discussion["name"]; + + $this->authorizeUser(); + $request = new Nette\Application\Request($this->getPresenterName(), 'GET', ['action' => 'discussion', 'discussion' => $discussionName]); + $response = $this->presenter->run($request); + + Assert::type('Nette\Application\Responses\TextResponse', $response); + + $re = '/&(?!(?:apos|quot|[gl]t|amp);|#)/'; + + $dom = NULL; + $html = (string)$response->getSource(); + //replace unescaped ampersands in html to prevent tests from failing + $html = preg_replace($re, "&", $html); + + $dom = DomQuery::fromHtml($html); + //has navbar + Assert::true($dom->has('div#snippet-navbar-nav')); + + //has breadcrumbs + + Assert::true($dom->has('div.container div.row div.col ol.breadcrumb')); + Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item a[href]')), 2); + Assert::equal(count($dom->find('ol.breadcrumb li.breadcrumb-item')), 3); //last item aint link + + Assert::equal(count($dom->find('div.container.my-2 div.row.justify-content-md-center')), $canWrite ? 2 : 1); + if($canWrite){ + Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 textarea#addPost')); + Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline input.form-control.mr-sm-2')); + Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline span.mr-auto input.form-control.btn.btn-outline-success.mr-sm-2')); + Assert::true($dom->has('div.container.my-2 div.row.justify-content-md-center div.col-md-10 div.addPost form.form-inline button.btn.btn-primary')); + } + + Assert::true($dom->has('div.container.discussion#snippet--discussion')); + Assert::true(count($dom->find('div.container.discussion#snippet--discussion div.row'))<=20); + } + + protected function getPresenterName(): string + { + return "Discussion:Default"; + } + + public function getModule(): string + { + return "Discussion"; + } +} + +(new DiscussionPresenterTest($container))->run(); diff --git a/app/module/autotest/app/discussion/DiscussionTest.php b/app/module/autotest/app/discussion/DiscussionTest.php index 6aaf8fda..7fd4c14c 100644 --- a/app/module/autotest/app/discussion/DiscussionTest.php +++ b/app/module/autotest/app/discussion/DiscussionTest.php @@ -4,13 +4,12 @@ namespace Tymy\Module\Autotest\Discussion; -use Tymy\Bootstrap; use Nette\Utils\DateTime; +use Tymy\Bootstrap; +use Tymy\Module\Autotest\ApiTest; +use Tymy\Module\Autotest\Entity\Assert; use Tymy\Module\Core\Model\BaseModel; use Tymy\Module\Discussion\Model\Discussion; -use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; -use Tymy\Module\Autotest\SimpleResponse; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -18,7 +17,7 @@ /** * Description of DiscussionTest */ -class DiscussionTest extends RequestCase +class DiscussionTest extends ApiTest { public function testGet(): void { @@ -44,7 +43,7 @@ public function testCRUDNotPermittedDiscussion(): void "writeRightName" => "ADMINONLY", "deleteRightName" => "ADMINONLY", "stickyRightName" => "ADMINMEMBER", - ]); + ])["id"]; $pid = $this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData()["id"]; @@ -98,7 +97,7 @@ public function testPostDiscussion(): void "writeRightName" => "ADMINONLY", "deleteRightName" => "ADMINONLY", "stickyRightName" => "ADMINMEMBER", - ]); + ])["id"]; $pid = $this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData()["id"]; @@ -200,7 +199,7 @@ public function testSticky(): void $dId = $this->recordManager->createDiscussion(null, [ "deleteRightName" => "ADMINONLY", "stickyRightName" => "ADMINMEMBER", - ]); + ])["id"]; $this->request($this->getBasePath() . "/$dId/post", "POST", ["post" => "ADMIN first post " . random_int(0, 10000)])->expect(201)->getData(); @@ -215,7 +214,7 @@ public function testSticky(): void public function testCRUD(): void { $this->authorizeAdmin(); - $recordId = $this->createRecord(); + $recordId = $this->createRecord()["id"]; $this->request($this->getBasePath() . "/" . $recordId)->expect(200, "array"); @@ -224,7 +223,7 @@ public function testCRUD(): void $this->deleteRecord($recordId); } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createDiscussion(); } diff --git a/app/module/autotest/app/event/EventTest.php b/app/module/autotest/app/event/EventTest.php index 041407bf..a3b2e2f2 100644 --- a/app/module/autotest/app/event/EventTest.php +++ b/app/module/autotest/app/event/EventTest.php @@ -5,8 +5,8 @@ namespace Tymy\Module\Autotest\Event; use Tymy\Bootstrap; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; use Tymy\Module\Event\Model\Event; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; @@ -15,7 +15,7 @@ /** * Description of EventTest */ -class EventTest extends RequestCase +class EventTest extends ApiTest { public function getModule(): string { @@ -80,7 +80,7 @@ public function testEventTypes(): void $this->request("eventTypes")->expect(200, "array"); } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createEvent(); } diff --git a/app/module/autotest/app/multiaccount/MultiaccountTest.php b/app/module/autotest/app/multiaccount/MultiaccountTest.php index e6b5f228..f980dd21 100644 --- a/app/module/autotest/app/multiaccount/MultiaccountTest.php +++ b/app/module/autotest/app/multiaccount/MultiaccountTest.php @@ -5,8 +5,8 @@ namespace Tymy\Module\Autotest\Multiaccount; use Tymy\Bootstrap; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; use Tymy\Module\Multiaccount\Model\TransferKey; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; @@ -17,7 +17,7 @@ * * @skip Skip this autotest, since its ot performable from cli - we would need two tymy instances to test this */ -class MultiaccountTest extends RequestCase +class MultiaccountTest extends ApiTest { public function getModule(): string { diff --git a/app/module/autotest/app/permission/PermissionTest.php b/app/module/autotest/app/permission/PermissionTest.php index 38c00952..5ba577d8 100644 --- a/app/module/autotest/app/permission/PermissionTest.php +++ b/app/module/autotest/app/permission/PermissionTest.php @@ -5,9 +5,9 @@ namespace Tymy\Module\Autotest\Permission; use Tymy\Bootstrap; -use Tymy\Module\Permission\Model\Permission; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; +use Tymy\Module\Permission\Model\Permission; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -15,7 +15,7 @@ /** * Description of PermissionTest */ -class PermissionTest extends RequestCase +class PermissionTest extends ApiTest { public function getModule(): string { @@ -71,7 +71,7 @@ public function testPermissionName(): void $this->request("permissionName/ADMINONLY")->expect(200, "array"); } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createPermission(); } diff --git a/app/module/autotest/app/poll/PollTest.php b/app/module/autotest/app/poll/PollTest.php index 3b3a2293..62ca6cc3 100644 --- a/app/module/autotest/app/poll/PollTest.php +++ b/app/module/autotest/app/poll/PollTest.php @@ -5,9 +5,9 @@ namespace Tymy\Module\Autotest\Permission; use Tymy\Bootstrap; -use Tymy\Module\Poll\Model\Poll; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; +use Tymy\Module\Poll\Model\Poll; require getenv("ROOT_DIR") . '/app/Bootstrap.php'; $container = Bootstrap::boot(); @@ -15,7 +15,7 @@ /** * Description of PollTest */ -class PollTest extends RequestCase +class PollTest extends ApiTest { public function getModule(): string { @@ -138,7 +138,7 @@ public function testVoting(): void //create poll and add some votes into it, the get the poll again and check the votes exists } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createPoll(); } diff --git a/app/module/autotest/app/user/UserTest.php b/app/module/autotest/app/user/UserTest.php index 7a845ed0..09f57793 100644 --- a/app/module/autotest/app/user/UserTest.php +++ b/app/module/autotest/app/user/UserTest.php @@ -6,9 +6,8 @@ use Nette\Security\AuthenticationException; use Tymy\Bootstrap; +use Tymy\Module\Autotest\ApiTest; use Tymy\Module\Autotest\Entity\Assert; -use Tymy\Module\Autotest\RequestCase; -use Tymy\Module\Autotest\SimpleResponse; use Tymy\Module\User\Model\User; use Vojir\Responses\CsvResponse\ComposedCsvResponse; @@ -18,7 +17,7 @@ /** * Description of UserTest */ -class UserTest extends RequestCase +class UserTest extends ApiTest { private bool $inited = false; @@ -336,7 +335,7 @@ public function testLive(): void $this->request("live", "POST")->expect(405); } - public function createRecord(): int + public function createRecord(): array { return $this->recordManager->createUser(); } diff --git a/app/module/autotest/entity/ApiTest.php b/app/module/autotest/entity/ApiTest.php new file mode 100644 index 00000000..49024792 --- /dev/null +++ b/app/module/autotest/entity/ApiTest.php @@ -0,0 +1,48 @@ +getModule(); + } + + public function deleteRecord($recordId): void + { + $this->recordManager->deleteRecord($this->getBasePath(), $recordId); + } + + //*************** COMMON TESTS, SAME FOR ALL MODULES + + public function testUnauthorized() + { + $this->user->logout(true); + + $this->request($this->getBasePath())->expect(401); + $this->request($this->getBasePath(), 'POST', $this->mockRecord())->expect(401); + $this->request($this->getBasePath() . "/1", 'PUT', $this->mockRecord())->expect(401); + $this->request($this->getBasePath() . "/1", 'DELETE')->expect(401); + } + + public function testMethodNotAllowed() + { + $this->authorizeAdmin(); + + $this->request($this->getBasePath(), 'HEAD')->expect(405); + + $this->user->logout(true); + } + //*************** END:COMMON TESTS +} diff --git a/app/module/autotest/entity/RequestCase.php b/app/module/autotest/entity/RequestCase.php index fa0faafe..93e7105d 100644 --- a/app/module/autotest/entity/RequestCase.php +++ b/app/module/autotest/entity/RequestCase.php @@ -44,7 +44,7 @@ abstract class RequestCase extends TestCase protected array $moduleConfig; protected RecordManager $recordManager; private Router $router; - private PresenterFactory $presenterFactory; + protected PresenterFactory $presenterFactory; protected AuthenticationManager $authenticationManager; protected Responder $responder; private MockRequestFactory $httpRequestFactory; @@ -57,15 +57,6 @@ abstract class RequestCase extends TestCase */ abstract public function getModule(): string; - abstract public function createRecord(); - - abstract public function mockRecord(); - - /** - * @return mixed[] - */ - abstract protected function mockChanges(): array; - public function __construct(protected Container $container) { define('TEST_DIR', Bootstrap::normalizePath(Bootstrap::MODULES_DIR . "/autotest")); @@ -83,16 +74,6 @@ public function __construct(protected Container $container) $this->httpRequestFactory = $this->container->getService("http.requestFactory"); } - protected function getBasePath(): string - { - return $this->getModule(); - } - - public function deleteRecord($recordId): void - { - $this->recordManager->deleteRecord($this->getBasePath(), $recordId); - } - public function getRecordManager(): RecordManager { return $this->recordManager; @@ -165,29 +146,6 @@ protected function change(int $recordId, ?array $changes = null) $this->assertObjectEquality($changes, $changedData); } - //*************** COMMON TESTS, SAME FOR ALL MODULES - - public function testUnauthorized() - { - $this->user->logout(true); - - $this->request($this->getBasePath())->expect(401); - $this->request($this->getBasePath(), 'POST', $this->mockRecord())->expect(401); - $this->request($this->getBasePath() . "/1", 'PUT', $this->mockRecord())->expect(401); - $this->request($this->getBasePath() . "/1", 'DELETE')->expect(401); - } - - public function testMethodNotAllowed() - { - $this->authorizeAdmin(); - - $this->request($this->getBasePath(), 'HEAD')->expect(405); - - $this->user->logout(true); - } - - //*************** END:COMMON TESTS - /** @return SimpleResponse */ public function request($url, $method = "GET", $data = [], $responseClass = null) { diff --git a/app/module/autotest/entity/UITest.php b/app/module/autotest/entity/UITest.php new file mode 100644 index 00000000..b6fddb90 --- /dev/null +++ b/app/module/autotest/entity/UITest.php @@ -0,0 +1,23 @@ +getPresenterName() != "undefined") { + $this->presenter = $this->presenterFactory->createPresenter($this->getPresenterName()); + $this->presenter->autoCanonicalize = false; + } + + parent::setUp(); + } + +} diff --git a/app/module/autotest/manager/RecordManager.php b/app/module/autotest/manager/RecordManager.php index 7db42593..50c9b7b5 100644 --- a/app/module/autotest/manager/RecordManager.php +++ b/app/module/autotest/manager/RecordManager.php @@ -21,7 +21,16 @@ public function __construct(private RequestCase $requestCase, private $config) { } - private function createRecord(string $url, array $data = null, $changes = null, string $identifier = "ID", ?array $checkSkips = null): int + /** + * Create record using modules url and important request parameters and return json serialized created record + * + * @param string $url + * @param array|null $data + * @param array|null $changes + * @param array|null $checkSkips + * @return array + */ + private function createRecord(string $url, ?array $data = null, ?array $changes = null, ?array $checkSkips = null): array { $this->applyChanges($data, $changes); @@ -29,11 +38,11 @@ private function createRecord(string $url, array $data = null, $changes = null, $this->requestCase->assertObjectEquality($data, $response->getData(), $checkSkips); - return $response->getData()[$identifier]; + return $response->getData(); } - /** @return int $recordId */ - public function createUser($data = null, $changes = null): int + /** @return array Created user */ + public function createUser($data = null, $changes = null): array { $postData = $data ?: $this->mockUser(); $this->applyChanges($postData, $changes); @@ -45,25 +54,25 @@ public function createUser($data = null, $changes = null): int Assert::equal(false, array_key_exists("password", $response->getData())); //check password is not returned Assert::equal(strtoupper($postData["login"]), $response->getData()["login"]); //check login has been saved in uppercase - return $response->getData()["id"]; + return $response->getData(); } - /** @return int $recordId */ - public function createDebt(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createDebt(?array $data = null, ?array $changes = null): array { - return $this->createRecord(Debt::MODULE, $data ?: $this->mockDebt(), $changes, "id"); + return $this->createRecord(Debt::MODULE, $data ?: $this->mockDebt(), $changes); } - /** @return int $recordId */ - public function createDiscussion(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createDiscussion(?array $data = null, ?array $changes = null): array { - return $this->createRecord(Discussion::MODULE, $data ?: $this->mockDiscussion(), $changes, "id"); + return $this->createRecord(Discussion::MODULE, $data ?: $this->mockDiscussion(), $changes); } - /** @return int $recordId */ - public function createEvent(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createEvent(?array $data = null, ?array $changes = null): array { - return $this->createRecord(Event::MODULE, $data ?: $this->mockEvent(), $changes, "id"); + return $this->createRecord(Event::MODULE, $data ?: $this->mockEvent(), $changes); } public function createOptions(int $pollId, int $numberOfOptions, ?array $data = null, ?array $changes = null): void @@ -81,28 +90,28 @@ public function createOptions(int $pollId, int $numberOfOptions, ?array $data = //here is record created directly, without checking the result, since we are creating array of objects and we cannot check that against returned array of objects (output contains ID, but input does not) } - /** @return int $recordId */ - public function createPermission(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createPermission(?array $data = null, ?array $changes = null): array { - return $this->createRecord(Permission::MODULE, $data ?: $this->mockPermission(), $changes, "id", ["allowedRoles", "allowedStatuses", "allowedUsers"]);//we are sending revocations, so there will not be any allowances + return $this->createRecord(Permission::MODULE, $data ?: $this->mockPermission(), $changes, ["allowedRoles", "allowedStatuses", "allowedUsers"]);//we are sending revocations, so there will not be any allowances } - /** @return int $recordId */ - public function createPoll(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createPoll(?array $data = null, ?array $changes = null): array { - return $this->createRecord(Poll::MODULE, $data ?: $this->mockPoll(), $changes, "id"); + return $this->createRecord(Poll::MODULE, $data ?: $this->mockPoll(), $changes); } - /** @return int $recordId */ - public function createStatus(int $statusSetId, ?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createStatus(int $statusSetId, ?array $data = null, ?array $changes = null): array { - return $this->createRecord("attendanceStatus", $data ?: $this->mockStatus($statusSetId), $changes, "id", ["image"]); + return $this->createRecord("attendanceStatus", $data ?: $this->mockStatus($statusSetId), $changes, ["image"]); } - /** @return int $recordId */ - public function createStatusSet(?array $data = null, ?array $changes = null): int + /** @return array Created record */ + public function createStatusSet(?array $data = null, ?array $changes = null): array { - return $this->createRecord("attendanceStatusSet", $data ?: $this->mockStatusSet(), $changes, "id"); + return $this->createRecord("attendanceStatusSet", $data ?: $this->mockStatusSet(), $changes); } public function mockAttendance(int $eventId, $pre = true, $post = false): array