From 94a8c7f3eb40cb2f17db175aeec0b737f3144cca Mon Sep 17 00:00:00 2001 From: Kartik Kathuria Date: Thu, 11 Nov 2021 02:07:54 +0530 Subject: [PATCH] browser test modified --- tests/phpunit/CRM/Extension/BrowserTest.php | 78 ++++++++++++++------- 1 file changed, 54 insertions(+), 24 deletions(-) diff --git a/tests/phpunit/CRM/Extension/BrowserTest.php b/tests/phpunit/CRM/Extension/BrowserTest.php index e826191eaf..6869b8c289 100644 --- a/tests/phpunit/CRM/Extension/BrowserTest.php +++ b/tests/phpunit/CRM/Extension/BrowserTest.php @@ -6,32 +6,62 @@ */ class CRM_Extension_BrowserTest extends CiviUnitTestCase { + use \Civi\Test\GuzzleTestTrait; + + /** + * @var CRM_Extension_Browser + */ + protected $browser; + + /** + * Get the expected response from browser extension. + * + * @return array + */ + protected function getExpectedResponse() { + return [ + '{"test.crm.extension.browsertest.a":"\n main<\/file>\n test_crm_extension_browsertest_a<\/name>\n Brought to you by the letter \"A\"<\/description>\n 0.1<\/version>\n http:\/\/example.com\/test.crm.extension.browsertest.a-0.1.zip<\/downloadUrl>\n \n test\/extension\/browsertest\/a<\/reportUrl>\n CiviContribute<\/component>\n <\/typeInfo>\n<\/extension>\n","test.crm.extension.browsertest.b":"\n moduletest<\/file>\n test_crm_extension_browsertest_b<\/name>\n 1.2<\/version>\n http:\/\/example.com\/test.crm.extension.browsertest.b-1.2.zip<\/downloadUrl>\n Brought to you by the letter \"B\"<\/description>\n<\/extension>\n"}', + ]; + } + + /** + * Add a mock handler to the extension browser for testing. + * + */ + protected function setupMockHandler() { + $responses = $this->getExpectedResponse(); + $this->createMockHandler($responses); + $this->setUpClientWithHistoryContainer(); + $this->browser->setGuzzleClient($this->getGuzzleClient()); + } + public function testDisabled() { - $browser = new CRM_Extension_Browser(FALSE, '/index.html', 'file:///itd/oesn/tmat/ter'); - $this->assertEquals(FALSE, $browser->isEnabled()); - $this->assertEquals([], $browser->checkRequirements()); - $this->assertEquals([], $browser->getExtensions()); + $this->browser = new CRM_Extension_Browser(FALSE, '/index.html', 'file:///itd/oesn/tmat/ter'); + $this->assertEquals(FALSE, $this->browser->isEnabled()); + $this->assertEquals([], $this->browser->checkRequirements()); + $this->assertEquals([], $this->browser->getExtensions()); } public function testCheckRequirements_BadCachedir_false() { - $browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, FALSE); - $this->assertEquals(TRUE, $browser->isEnabled()); - $reqs = $browser->checkRequirements(); + $this->browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, FALSE); + $this->assertEquals(TRUE, $this->browser->isEnabled()); + $reqs = $this->browser->checkRequirements(); $this->assertEquals(1, count($reqs)); } public function testCheckRequirements_BadCachedir_nonexistent() { - $browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, '/tot/all/yin/v/alid'); - $this->assertEquals(TRUE, $browser->isEnabled()); - $reqs = $browser->checkRequirements(); + $this->browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, '/tot/all/yin/v/alid'); + $this->assertEquals(TRUE, $this->browser->isEnabled()); + $reqs = $this->browser->checkRequirements(); $this->assertEquals(1, count($reqs)); } public function testGetExtensions_good() { - $browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); - $this->assertEquals(TRUE, $browser->isEnabled()); - $this->assertEquals([], $browser->checkRequirements()); - $exts = $browser->getExtensions(); + $this->browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); + $this->assertEquals(TRUE, $this->browser->isEnabled()); + $this->assertEquals([], $this->browser->checkRequirements()); + $this->setupMockHandler(); + $exts = $this->browser->getExtensions(); $keys = array_keys($exts); sort($keys); $this->assertEquals(['test.crm.extension.browsertest.a', 'test.crm.extension.browsertest.b'], $keys); @@ -42,21 +72,21 @@ class CRM_Extension_BrowserTest extends CiviUnitTestCase { } public function testGetExtension_good() { - $browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); - $this->assertEquals(TRUE, $browser->isEnabled()); - $this->assertEquals([], $browser->checkRequirements()); - - $info = $browser->getExtension('test.crm.extension.browsertest.b'); + $this->browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); + $this->assertEquals(TRUE, $this->browser->isEnabled()); + $this->assertEquals([], $this->browser->checkRequirements()); + $this->setupMockHandler(); + $info = $this->browser->getExtension('test.crm.extension.browsertest.b'); $this->assertEquals('module', $info->type); $this->assertEquals('http://example.com/test.crm.extension.browsertest.b-1.2.zip', $info->downloadUrl); } public function testGetExtension_nonexistent() { - $browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); - $this->assertEquals(TRUE, $browser->isEnabled()); - $this->assertEquals([], $browser->checkRequirements()); - - $info = $browser->getExtension('test.crm.extension.browsertest.nonexistent'); + $this->browser = new CRM_Extension_Browser('file://' . dirname(__FILE__) . '/dataset/good-repository', NULL, $this->createTempDir('ext-cache-')); + $this->assertEquals(TRUE, $this->browser->isEnabled()); + $this->assertEquals([], $this->browser->checkRequirements()); + $this->setupMockHandler(); + $info = $this->browser->getExtension('test.crm.extension.browsertest.nonexistent'); $this->assertEquals(NULL, $info); } -- 2.25.1