From 4e953b34597b75a08e13a62076a8f123cb65e511 Mon Sep 17 00:00:00 2001 From: John Linhart Date: Mon, 15 Jun 2026 11:25:14 +0200 Subject: [PATCH 1/2] Stage weight must be unique to pass the validation --- tests/Api/MauticApiTestCase.php | 24 +++++++++++++++--------- tests/Api/StagesTest.php | 15 +++++++++++++-- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/tests/Api/MauticApiTestCase.php b/tests/Api/MauticApiTestCase.php index c1a4fb1..29e1d56 100644 --- a/tests/Api/MauticApiTestCase.php +++ b/tests/Api/MauticApiTestCase.php @@ -173,13 +173,19 @@ protected function assertPayloadItem($itemProp, $callback, $itemVal, $item) } } + protected function getTestPayload(): array + { + return $this->testPayload; + } + protected function standardTestGetListOfSpecificIds($callback = null) { // Create some items first $itemIds = []; for ($i = 0; $i <= 2; ++$i) { - $response = $this->api->create($this->testPayload); - $this->assertErrors($response, 'Payload: '.json_encode($this->testPayload, JSON_PRETTY_PRINT)); + $payload = $this->getTestPayload(); + $response = $this->api->create($payload); + $this->assertErrors($response, 'Payload: '.json_encode($payload, JSON_PRETTY_PRINT)); $itemIds[] = $response[$this->api->itemName()]['id']; } @@ -233,7 +239,7 @@ protected function standardTestGetList() protected function standardTestCreateGetAndDelete(?array $payload = null) { if (empty($payload)) { - $payload = $this->testPayload; + $payload = $this->getTestPayload(); } // Create item @@ -259,9 +265,9 @@ protected function standardTestBatchEndpoints(?array $batch = null, $callback = if (null == $batch) { $batch = [ - $this->testPayload, - $this->testPayload, - $this->testPayload, + $this->getTestPayload(), + $this->getTestPayload(), + $this->getTestPayload(), ]; } @@ -304,12 +310,12 @@ protected function standardTestBatchEndpoints(?array $batch = null, $callback = public function standardTestEditPatch(array $editTo) { - $response = $this->api->edit(10000, $this->testPayload); + $response = $this->api->edit(10000, $this->getTestPayload()); // there should be an error as the item shouldn't exist $this->assertTrue(isset($response['errors'][0]), $response['errors'][0]['message']); - $response = $this->api->create($this->testPayload); + $response = $this->api->create($this->getTestPayload()); $this->assertPayload($response); $response = $this->api->edit($response[$this->api->itemName()]['id'], $editTo); @@ -321,7 +327,7 @@ public function standardTestEditPatch(array $editTo) public function standardTestEditPut() { - $response = $this->api->edit(10000, $this->testPayload, true); + $response = $this->api->edit(10000, $this->getTestPayload(), true); $this->assertPayload($response); // now delete the entity diff --git a/tests/Api/StagesTest.php b/tests/Api/StagesTest.php index 20befa1..3c3e61c 100644 --- a/tests/Api/StagesTest.php +++ b/tests/Api/StagesTest.php @@ -13,6 +13,8 @@ class StagesTest extends MauticApiTestCase { + private static $weightCounter = 0; + public function setUp(): void { $this->api = $this->getContext('stages'); @@ -21,6 +23,14 @@ public function setUp(): void ]; } + protected function getTestPayload(): array + { + return [ + 'name' => sprintf('test %s', uniqid('', true)), + 'weight' => (int) (microtime(true) * 1000000) + ++self::$weightCounter, + ]; + } + public function testGetList() { $this->standardTestGetList(); @@ -58,8 +68,9 @@ public function testAddAndRemove() $contact = $response['contact']; // Create stage - $response = $this->api->create($this->testPayload); - $this->assertPayload($response); + $payload = $this->getTestPayload(); + $response = $this->api->create($payload); + $this->assertPayload($response, $payload); $stage = $response[$this->api->itemName()]; // Add contact to the stage From ec3ab767db5ce8e754b9af450639f19e3cc36c71 Mon Sep 17 00:00:00 2001 From: John Linhart Date: Mon, 15 Jun 2026 11:38:59 +0200 Subject: [PATCH 2/2] the weights were too long, not in all places --- tests/Api/MauticApiTestCase.php | 12 +++++++----- tests/Api/StagesTest.php | 17 +++++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/tests/Api/MauticApiTestCase.php b/tests/Api/MauticApiTestCase.php index 29e1d56..c1362a2 100644 --- a/tests/Api/MauticApiTestCase.php +++ b/tests/Api/MauticApiTestCase.php @@ -310,13 +310,14 @@ protected function standardTestBatchEndpoints(?array $batch = null, $callback = public function standardTestEditPatch(array $editTo) { - $response = $this->api->edit(10000, $this->getTestPayload()); + $createPayload = $this->getTestPayload(); + $response = $this->api->edit(10000, $createPayload); // there should be an error as the item shouldn't exist $this->assertTrue(isset($response['errors'][0]), $response['errors'][0]['message']); - $response = $this->api->create($this->getTestPayload()); - $this->assertPayload($response); + $response = $this->api->create($createPayload); + $this->assertPayload($response, $createPayload); $response = $this->api->edit($response[$this->api->itemName()]['id'], $editTo); $this->assertPayload($response, $editTo); @@ -327,8 +328,9 @@ public function standardTestEditPatch(array $editTo) public function standardTestEditPut() { - $response = $this->api->edit(10000, $this->getTestPayload(), true); - $this->assertPayload($response); + $payload = $this->getTestPayload(); + $response = $this->api->edit(10000, $payload, true); + $this->assertPayload($response, $payload); // now delete the entity $response = $this->api->delete($response[$this->api->itemName()]['id']); diff --git a/tests/Api/StagesTest.php b/tests/Api/StagesTest.php index 3c3e61c..556fde0 100644 --- a/tests/Api/StagesTest.php +++ b/tests/Api/StagesTest.php @@ -13,7 +13,7 @@ class StagesTest extends MauticApiTestCase { - private static $weightCounter = 0; + private static $nextWeight; public function setUp(): void { @@ -27,10 +27,23 @@ protected function getTestPayload(): array { return [ 'name' => sprintf('test %s', uniqid('', true)), - 'weight' => (int) (microtime(true) * 1000000) + ++self::$weightCounter, + 'weight' => $this->getNextWeight(), ]; } + private function getNextWeight(): int + { + if (null === self::$nextWeight) { + $response = $this->api->getList('', 0, 1, 'weight', 'DESC'); + $firstStage = $response[$this->api->listName()][0] ?? []; + $maxWeight = isset($firstStage['weight']) ? (int) $firstStage['weight'] : 0; + + self::$nextWeight = $maxWeight + 1; + } + + return self::$nextWeight++; + } + public function testGetList() { $this->standardTestGetList();