automationStorage = $this->diContainer->get(AutomationStorage::class); $id = $this->automationStorage->createAutomation( new Automation( 'Testing automation', ['root' => new Step('root', Step::TYPE_ROOT, 'core:root', [], [])], wp_get_current_user() ) ); $automation = $this->automationStorage->getAutomation($id); $this->assertInstanceOf(Automation::class, $automation); $this->automation = $automation; } public function testEditorIsAllowed(): void { wp_set_current_user($this->editorUserId); $data = $this->post(sprintf(self::ENDPOINT_PATH, $this->automation->getId())); $this->assertSame('Copy of Testing automation', $data['data']['name']); $this->assertNotNull($this->automationStorage->getAutomation($this->automation->getId() + 1)); } public function testGuestNotAllowed(): void { wp_set_current_user(0); $data = $this->post(sprintf(self::ENDPOINT_PATH, $this->automation->getId())); $this->assertSame([ 'code' => 'rest_forbidden', 'message' => 'Sorry, you are not allowed to do that.', 'data' => ['status' => 401], ], $data); $automation = $this->automationStorage->getAutomation($this->automation->getId()); $this->assertInstanceOf(Automation::class, $automation); $this->assertSame('Testing automation', $automation->getName()); $this->assertNull($this->automationStorage->getAutomation($this->automation->getId() + 1)); } public function testItDuplicatesAnAutomation(): void { $data = $this->post(sprintf(self::ENDPOINT_PATH, $this->automation->getId())); $id = $this->automation->getId() + 1; $user = wp_get_current_user(); $createdAt = DateTimeImmutable::createFromFormat(DateTimeImmutable::W3C, $data['data']['created_at'] ?? null); $updatedAt = DateTimeImmutable::createFromFormat(DateTimeImmutable::W3C, $data['data']['updated_at'] ?? null); $this->assertInstanceOf(DateTimeImmutable::class, $createdAt); $this->assertInstanceOf(DateTimeImmutable::class, $updatedAt); $this->assertEquals($createdAt, $updatedAt); $expected = [ 'id' => $id, 'name' => 'Copy of Testing automation', 'status' => 'draft', 'created_at' => $createdAt->format(DateTimeImmutable::W3C), 'updated_at' => $updatedAt->format(DateTimeImmutable::W3C), 'activated_at' => null, 'author' => [ 'id' => $user->ID, 'name' => $user->display_name, ], 'stats' => [ 'automation_id' => $id, 'totals' => [ 'entered' => 0, 'in_progress' => 0, 'exited' => 0, ], ], 'steps' => [ 'root' => [ 'id' => 'root', 'type' => 'root', 'key' => 'core:root', 'args' => [], 'next_steps' => [], ], ], 'meta' => [], ]; $this->assertSame(['data' => $expected], $data); $expectedAutomation = Automation::fromArray( array_merge($expected, ['steps' => json_encode($expected['steps']), 'version_id' => 1]) ); $automation = $this->automationStorage->getAutomation($id); $this->assertInstanceOf(Automation::class, $automation); $this->assertTrue($automation->equals($expectedAutomation)); } }