Skip to content
Snippets Groups Projects
RestApiTest.php 3.19 KiB
Newer Older
Prasad's avatar
Prasad committed
<?php
/**
 * The content of this file is subject to ("Vtiger Public License 1.2") refer to LIECENSE.md for more details.
 * Copyright (c) Vtiger.
 * All Rights Reserved.
 */

class RestApiTest extends \PHPUnit\Framework\TestCase {

	private $restapi;
	private $MOCK_TEST_ACCOUNTNAME = "RestApiTestAccount";

	protected function setUp() {

		$this->assertNotEmpty($_ENV['CRM_DOMAIN'], "CRM_DOMAIN not configured in phpunit.xml");
		$this->assertNotEmpty($_ENV['CRM_USERNAME'], "CRM_USERNAME not configured in phpunit.xml");
		$this->assertNotEmpty($_ENV['CRM_ACCESSKEY'], "CRM_ACCESSKEY not configured in phpunit.xml");

		$this->restapi = new \Vtiger\Cloud\RestApi(
			$_ENV['CRM_DOMAIN'],
			$_ENV['CRM_USERNAME'],
			$_ENV['CRM_ACCESSKEY']
		);
	}

	protected function fetchMockAccountRecord() {
		$records = $this->restapi->query("SELECT * FROM Accounts WHERE accountname='$this->MOCK_TEST_ACCOUNTNAME' LIMIT 1;");
		$this->assertNotEmpty($records, "Failed to fetch mock test record.");
		return array_shift($records);
	}

	public function testMe() {
		$this->assertNotNull($this->restapi->me());
	}

	public function testListTypes() {
		$this->assertNotNull($this->restapi->list_types());
	}

	public function testDescribe() {
		$this->assertNotNull($this->restapi->describe("Accounts"));
	}

	public function testCreate() {
		$this->assertNotNull($this->restapi->create("Accounts", array(
			"accountname" => $this->MOCK_TEST_ACCOUNTNAME,
			"assigned_user_id" => $this->restapi->myid()
		)));
	}

	public function testUpdate() {
		$record = $this->fetchMockAccountRecord();
		$record["website"]  = "www.404.com";
		$this->assertNotNull($this->restapi->update($record));
	}

	public function testRevise() {
		$record = $this->fetchMockAccountRecord();
		$this->assertNotNull($this->restapi->revise(array(
			"id" => $record["id"],
			"website" => "www.404-rev.com"
		)));
	}
	
	public function testQuery() {
		$this->fetchMockAccountRecord();
	}

	public function testTagsAdd() {
		$record = $this->fetchMockAccountRecord();
		$this->assertNotNull(
			$this->restapi->tags_add($record["id"], array("test-rest-tag1", "test-rest-tag2"))
		);
	}

	public function testTagsDelete() {
		$record = $this->fetchMockAccountRecord();
		$this->assertNotEmpty(
			$this->restapi->tags_delete($record["id"], array("test-rest-tag1"))
		);
	}

	public function testTagsRetrieve() {
		$record = $this->fetchMockAccountRecord();
		$this->assertNotEmpty(
			$this->restapi->tags_retrieve($record["id"])
		);
	}

	public function testRelatedTypes() {
		$this->assertNotNull(
			$this->restapi->related_types("Accounts")
		);
	}

	public function xtestDelete() {
		$record = $this->fetchMockAccountRecord();
		$this->assertNotNull($this->restapi->delete($record["id"]));
	}

	public function testConvertLead() {
		$lead = $this->restapi->create("Leads", array(
			"lastname" => "LEAD-".microtime(),
			"firstname"=> "RestApiTest",
			"company"  => "RestApiTestCompany-". microtime(),
			"email"    => "lead-".microtime()."@404.com",
			"assigned_user_id" => $this->restapi->myid()
		));
		$this->assertNotEmpty($lead["id"]);
		$result = $this->restapi->convert_lead($lead["id"], array("closingdate" => date("YYYY-MM-dd")));
		$this->assertNotEmpty($result["Potentials"]);
	}
}