diff --git a/Cpanel/Parser/JSON.php b/Cpanel/Parser/JSON.php index a592307..14fb6a9 100644 --- a/Cpanel/Parser/JSON.php +++ b/Cpanel/Parser/JSON.php @@ -134,6 +134,11 @@ public function parse($str) self::ERROR_DECODE, 'Cannot decode empty string.' ); } + + if (function_exists('iconv')) { + $str = iconv('UTF-8', 'UTF-8//IGNORE', utf8_encode($str)); + } + $r = json_decode($str, true); if (is_null($r)) { $this->_hasParseError = true; diff --git a/Cpanel/PublicAPI.php b/Cpanel/PublicAPI.php index a752662..c7731ee 100644 --- a/Cpanel/PublicAPI.php +++ b/Cpanel/PublicAPI.php @@ -54,6 +54,7 @@ * @version 0.1.0 * @link http://sdk.cpanel.net * @since 0.1.0 + * */ class Cpanel_PublicAPI extends Cpanel_Core_Object { diff --git a/Cpanel/Query/Http/Abstract.php b/Cpanel/Query/Http/Abstract.php index 2da7666..88cc971 100644 --- a/Cpanel/Query/Http/Abstract.php +++ b/Cpanel/Query/Http/Abstract.php @@ -606,12 +606,10 @@ public function buildAuthStr() return 'Authorization: WHM ' . $this->user . ':' - . $this->auth - . "\r\n"; + . $this->auth; } elseif ($this->auth_type == 'pass') { return 'Authorization: Basic ' - . base64_encode($this->user . ':' . $this->auth) - . "\r\n"; + . base64_encode($this->user . ':' . $this->auth); } else { THROW new Exception('invalid auth_type set'); } @@ -748,6 +746,7 @@ public function curlQuery($rObj) curl_setopt($curl, CURLOPT_BUFFERSIZE, 131072); // Pass authentication header $curlHeader = $this->buildCurlHeaders($curl, $rObj); + $url = $rObj->query->url; // Set the URL curl_setopt($curl, CURLOPT_URL, $url); @@ -760,7 +759,9 @@ public function curlQuery($rObj) . "\tAUTH: {$rObj->query->authstr}\n" ); } + $result = $this->curlExec($curl); + curl_close($curl); return $result; } @@ -782,6 +783,7 @@ protected function curlExec($curl) throw new Exception('Invalid cURL resource'); } $result = curl_exec($curl); + if ($result === false) { $msg = 'curl_exec threw error "' . curl_error($curl) . '"'; $rObj = $this->getResponseObject(); @@ -801,21 +803,20 @@ protected function curlExec($curl) * Update cURL resource POST field data and return a header string * * @param resource $curl cURL resource to update - * @param string $headerStr Previous built header string * @param string $postdata URL query parameter string to append to header * string * * @return string Appended header string * @throws Exception If $curl is not a resource */ - protected function addCurlPostFields($curl, $headerStr, $postdata) + protected function addCurlPostFields($curl, $postdata) { if (!is_resource($curl)) { throw new Exception('Invalid cURL resource'); } + curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); - return $headerStr . "\r\n" . $postdata;; } /** * Build a cURL header array, updating cURL POST field data as necessasry @@ -847,20 +848,23 @@ protected function buildCurlHeaders($curl, $rObj) $customHeaders = $h->getAllDataRecursively(); $headers = $customHeaders + $headers; } + + $curlHeader[0] = $authstr; + foreach ($headers as $key => $value) { - $headerStrs[] = "{$key}: {$value}"; + $curlHeader[] = "{$key}: {$value}"; } - $curlHeader[0] = $authstr . implode("\r\n", $headerStrs); + $qt = $queryObj->httpQueryType; if ($qt && strtoupper($qt) == 'GET') { $queryObj->url = "{$queryObj->url}?{$postdata}"; } else { - $curlHeader[0] = $this->addCurlPostFields( + $this->addCurlPostFields( $curl, - $curlHeader[0], $postdata ); } + return $curlHeader; } /** diff --git a/Cpanel/Service/Abstract.php b/Cpanel/Service/Abstract.php index c0f1efe..b30781a 100644 --- a/Cpanel/Service/Abstract.php +++ b/Cpanel/Service/Abstract.php @@ -515,7 +515,8 @@ protected function arrayType($arr) ); } ksort($arr); - $fkey = array_shift(array_keys($arr)); + $arrKeys = array_keys($arr); + $fkey = array_shift($arrKeys); if (is_int($fkey)) { return self::API1ARGS; } diff --git a/Cpanel/Service/Adapter/WHMapi.php b/Cpanel/Service/Adapter/WHMapi.php index 463131b..ffa351d 100644 --- a/Cpanel/Service/Adapter/WHMapi.php +++ b/Cpanel/Service/Adapter/WHMapi.php @@ -67,24 +67,26 @@ class Cpanel_Service_Adapter_WHMapi extends Cpanel_Service_XmlapiClientClass 'JSON', 'XML' ); + /** * Constructor - * + * * Prepare the adapter for use by Service. - * + * * If $RFT is not passed the const Cpanel_Service_Adapter_WHMapi::DRFT will be - * used when invoking {@link setAdapterResponseFormatType()} at + * used when invoking {@link setAdapterResponseFormatType()} at * instantiation - * + * * HTTP port is set to '2087' by default. {@link setPort()} - * + * * NOTE: this constructor as support for the legacy PHP XML-API client class - * + * * @param string $host Host address for query call * @param string $user User to authenticate query call * @param string $password Password to authenticate query call * @param string $RFT Response format type - * + * + * @throws Exception in case of no valid stread wrapper is found * @return Cpanel_Service_Adapter_WHMapi */ public function __construct($host = null, $user = null, $password = null, $RFT = null) @@ -99,7 +101,19 @@ public function __construct($host = null, $user = null, $password = null, $RFT = if ($password) { $this->setPassword($password); } - $this->setPort('2087'); + + // @codeCoverageIgnoreStart + $registeredStreams = stream_get_wrappers(); + if (in_array('https', $registeredStreams)) { + $port = 2087; + } elseif (in_array('http', $registeredStreams)) { + $port = 2086; + } else { + throw new Exception('No valid protocol stream wrapper registered'); + } + // @codeCoverageIgnoreEnd + + $this->setPort($port); $RFT = ($RFT) ? $RFT : self::DRFT; $this->setAdapterResponseFormatType($RFT); return $this; diff --git a/Cpanel/Tests/Cpanel/Core/ObjectTest.php b/Cpanel/Tests/Cpanel/Core/ObjectTest.php index 78b69e8..81d0b24 100644 --- a/Cpanel/Tests/Cpanel/Core/ObjectTest.php +++ b/Cpanel/Tests/Cpanel/Core/ObjectTest.php @@ -1,5 +1,4 @@ getRObj(); $rObj->setResponse($data); - $r = $p->encodeQueryObject($rObj); + + $r = $p->encodeQueryObject(serialize($rObj)); } public function testEncodeQueryObjectCallsGetResponseAndEncodes() { @@ -483,7 +484,9 @@ public function testEncodeQueryObjectCallsGetResponseAndEncodes() )); $rObj->expects($this->once())->method('getResponse')->with('array')->will($this->returnValue($this->getArrayListPopsWithDisk())); $expected = $this->getXMLListPopsWithDisk(); + $actual = $p->encodeQueryObject($rObj); + $this->assertEquals($expected, $actual); } public function testEncodeQueryObjectCallsGetResponseAndEncodesSmall() @@ -682,7 +685,7 @@ public function getXMLListPopsWithDisk() 2 <_diskquota>262144000 - <_diskused> + <_diskused/> 250 250 auththis diff --git a/Cpanel/Tests/Cpanel/PublicAPITest.php b/Cpanel/Tests/Cpanel/PublicAPITest.php index da120c1..abc270a 100644 --- a/Cpanel/Tests/Cpanel/PublicAPITest.php +++ b/Cpanel/Tests/Cpanel/PublicAPITest.php @@ -1,5 +1,4 @@ cut, 'user'); $rprop->setAccessible(true); $rprop->setValue($rq, $user); - $expected = "Authorization: WHM ${user}:${auth}\r\n"; + $expected = "Authorization: WHM ${user}:${auth}"; $actual = $rq->buildAuthStr(); $this->assertEquals($expected, $actual); } @@ -996,7 +996,7 @@ public function testBuildAuthStrReturnsPasswordAuthStr() $rprop = new ReflectionProperty($this->cut, 'user'); $rprop->setAccessible(true); $rprop->setValue($rq, $user); - $expected = "Authorization: Basic " . base64_encode($user . ':' . $auth) . "\r\n"; + $expected = "Authorization: Basic " . base64_encode($user . ':' . $auth); $actual = $rq->buildAuthStr(); $this->assertEquals($expected, $actual); } @@ -1310,12 +1310,12 @@ public function testAddCurlPostFieldsParams() $params = $rmeth->getParameters(); $expected = array( 0 => 'curl', - 1 => 'headerStr', - 2 => 'postdata' + 1 => 'postdata' ); foreach ($params as $param) { $actual[$param->getPosition() ] = $param->getName(); } + $this->assertEquals($expected, $actual); } /** @@ -1333,8 +1333,9 @@ public function testAddCurlPostFieldsReturnsString() $rq = $this->getRQ(); $rmeth = new ReflectionMethod($this->cut, 'addCurlPostFields'); $rmeth->setAccessible(true); - $actual = $rmeth->invoke($rq, curl_init(), '', 'what=ever'); - $this->assertInternalType('string', $actual); + $actual = $rmeth->invoke($rq, curl_init(), 'what=ever'); + + $this->assertInternalType('null', $actual); } /** * @expectedException Exception @@ -1403,15 +1404,13 @@ public function testBuildCurlHeadersDoesNotAlterURLForPOST() $actual = $rmeth->invoke($rq, $curl, $rObj); $this->assertEquals($url, $rObj->query->url); } - /** - * @depends testAddCurlPostFieldsReturnsString - */ + public function testBuildCurlHeadersCallsAddCurlPostFields() { $user = 'foo'; $pass = 'bar'; $postdata = 'what=ever'; - $authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass) . "\r\n"; + $authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass); $customHeaders = array( 'CustomHeader' => 'CustomHeaderValue' ); @@ -1421,7 +1420,7 @@ public function testBuildCurlHeadersCallsAddCurlPostFields() $rq = $this->getRQ(array( 'addCurlPostFields' )); - $rq->expects($this->once())->method('addCurlPostFields')->with($curl, $this->anything(), $postdata)->will($this->returnValue($this->anything())); + $rObj = $this->getRObj(); // $rObj->setQuery($customHeaders); $rObj->query->authstr = $authstr; @@ -1438,7 +1437,7 @@ public function testBuildCurlHeadersAddsCustomHeaders() $user = 'foo'; $pass = 'bar'; $postdata = 'what=ever'; - $authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass) . "\r\n"; + $authstr = 'Authentication: Basic ' . base64_encode($user . ':' . $pass); $customHeaders = array( 'CustomHeader' => 'CustomHeaderValue' ); @@ -1454,7 +1453,8 @@ public function testBuildCurlHeadersAddsCustomHeaders() $rmeth = new ReflectionMethod($this->cut, 'buildCurlHeaders'); $rmeth->setAccessible(true); $actual = $rmeth->invoke($rq, $curl, $rObj); - $containsHeaders = (bool)strpos($actual[0], 'CustomHeader: CustomHeaderValue'); + + $containsHeaders = ($actual[1] == 'CustomHeader: CustomHeaderValue'); $this->assertTrue($containsHeaders); } public function testBuildCurlHeadersAltersURLForGET() diff --git a/Cpanel/Tests/Cpanel/Query/Live/AbstractTest.php b/Cpanel/Tests/Cpanel/Query/Live/AbstractTest.php index 5c560b1..c37fba4 100644 --- a/Cpanel/Tests/Cpanel/Query/Live/AbstractTest.php +++ b/Cpanel/Tests/Cpanel/Query/Live/AbstractTest.php @@ -99,7 +99,10 @@ public static function startMSS() if (!file_exists($mockserverscript)) { self::fail("Mock socket server script '$mockserverscript' does not exist"); } - $cmd = "/usr/bin/php -f $mockserverscript"; + + $phpPath = exec('which php'); + $cmd = "$phpPath -f $mockserverscript"; //original + $arg = "socketfile={$socketfile}"; $full_cmd = "nohup $cmd $arg > /dev/null 2>&1 & echo $!"; // > /dev/null $PID = exec($full_cmd); @@ -369,7 +372,7 @@ public function testOpenCpanelHandleThrowsOnBadSocketfile() * looks like this test passed due to warn by fsockopen, and not the scripted * exception throw * @todo consider reworking or removing; might be untestable - * @expectedException Exception + * @expectedException PHPUnit_Framework_Error_Warning */ public function testOpenCpanelHandleThrowsOnBadfsockopen() { diff --git a/Cpanel/Tests/Cpanel/Service/AbstractTest.php b/Cpanel/Tests/Cpanel/Service/AbstractTest.php new file mode 100644 index 0000000..ddb2a2a --- /dev/null +++ b/Cpanel/Tests/Cpanel/Service/AbstractTest.php @@ -0,0 +1,1724 @@ +getMock($this->cut, $methods, $args, $mockName, $callConst, $callClone, $callA); + return $m; + } + /** + * @param bool $mock Return a PHPUnit mock object + * @param unknown_type $methods + * @param unknown_type $args + * @param unknown_type $mockName + * @param unknown_type $callConst + * @param unknown_type $callClone + * @param unknown_type $callA + * + * + * @return Cpanel_Query_Object + */ + public function getRObj($mock = false, $methods = array(), $args = array(), $mockName = '', $callConst = true, $callClone = true, $callA = true) + { + if ($mock) { + if (empty($methods)) { + $methods = null; + } + return $this->getMock($this->qa, $methods, $args, $mockName, $callConst, $callClone, $callA); + } + return new Cpanel_Query_Object(); + } + + public function tearDown() + { + self::restoreHash(); + } + + public function createHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $hash = "foo\nbar\n"; + $fh = fopen($filename, 'w+'); + fwrite($fh, $hash); + fclose($fh); + return $hash; + } + public function removeHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $filenameBck = $filename."_bck"; + + if (file_exists($filename)) { + if(!rename($filename, $filenameBck)) { + self::fail("Can't create backup file of $filename"); + exit(); + } + } + } + + public function restoreHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $filenameBck = $filename."_bck"; + + if (file_exists($filenameBck)) { + if(!rename($filenameBck, $filename)) { + self::fail("Can't restore file $filename. Please restore it manually from $filenameBck"); + exit(); + } + } + } + + public function setEnvironmentVars($password = false, $server = 'cpsrvd') + { + if ($password) { + putenv('REMOTE_PASSWORD=' . $password); + } elseif (is_null($password)) { + //set to blank + putenv('REMOTE_PASSWORD='); + } elseif ($password === false) { + putenv('REMOTE_PASSWORD=__HIDDEN__'); + } + putenv('SERVER_SOFTWARE=' . $server); + } + public function removeEnvironmentVars() + { + putenv('REMOTE_PASSWORD'); + putenv('SERVER_SOFTWARE'); + } + public function setLocal() + { + $filename = '/tmp/publicapi.test.sock'; + putenv('CPANEL_PHPCONNECT_SOCKET=' . $filename); + touch($filename); + } + public function unsetLocal() + { + $filename = '/tmp/publicapi.test.sock'; + putenv('CPANEL_PHPCONNECT_SOCKET'); + if (file_exists($filename)) { + unlink($filename); + } + } + public function testHasAbstractMethods() + { + $expected = array( + 'getDefaultAdapterName', + 'validAdapter', + 'spawnAdapter', + ); + sort($expected); + $rclass = new ReflectionClass($this->real_cut); + $methods = $rclass->getMethods(); + foreach ($methods as $meth) { + if ($meth->isAbstract()) { + $actual[] = $meth->getName(); + } + } + sort($actual); + $this->assertEquals($expected, $actual); + } + /** + * @expectedException Exception + * @depends testHasAbstractMethods + */ + public function testConstructThrowsOnBadInput() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($this->cut, '__construct'); + $rmeth->invoke($cs, 'fakestring'); + } + public function classConstants() + { + return array( + array( + 'API1ARGS', + 'ordinal' + ), + array( + 'API2ARGS', + 'associative' + ), + array( + 'ADAPTER_CPANEL', + 'cpanel' + ), + array( + 'ADAPTER_WHM', + 'whostmgr' + ), + array( + 'ADAPTER_LIVE', + 'live' + ), + ); + } + /** + * @dataProvider classConstants + * + */ + public function testClassHasConstants($name, $value) + { + $rclass = new ReflectionClass($this->real_cut); + $actual = $rclass->getConstants(); + $this->assertArrayHasKey($name, $actual); + $this->assertEquals($value, $actual[$name]); + } + /** + * + */ + public function testInstantiationSetsALogger() + { + $cs = new $this->cut(); + $this->assertInstanceOf('Cpanel_Listner_Subject_Abstract', $cs->listner); + } + /** + * @expectedException Exception + */ + public function testDisableAdapterThrowOnBadType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue(false)); + $cs->disableAdapter('cpanel'); + } + public function testDisableAdapterStoresOnValidType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue('foo')); + $cs->disableAdapter('cpanel'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $da = $rprop->getValue($cs); + $this->assertTrue(in_array('foo', $da)); + } + /** + * @expectedException Exception + */ + public function testEnableAdapterThrowOnBadType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue(false)); + $cs->enableAdapter('cpanel'); + } + public function testEnableAdapterRemovesOnValidType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue('foo')); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + 'foo' + )); + $cs->enableAdapter('cpanel'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $da = $rprop->getValue($cs); + $this->assertFalse(in_array('foo', $da)); + } + public function testPrivategetEUIDAuthReturnsArray() + { + $this->removeHash(); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertInternalType('array', $r); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithParticularKeys() + { + $this->removeHash(); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithHashWhenAvailable() + { + $this->removeHash(); + $hash = $this->createHash(); + $expected = array( + 'hash' => $hash, + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithPasswordWhenAvailable() + { + $this->removeHash(); + $this->setEnvironmentVars('foo'); + $expected = array( + 'hash' => '', + 'password' => 'foo' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenNoPassword() + { + $this->removeHash(); + $this->setEnvironmentVars(null); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenPasswordHidden() + { + $this->removeHash(); + $this->setEnvironmentVars(false); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenNotCpsrvd() + { + $this->removeHash(); + $this->setEnvironmentVars('foo', 'webmail'); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivateGetEnvironmentContextAlwaysReturnsArrayWithKeys() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + foreach ($expected as $key => $value) { + $this->assertArrayHasKey($key, $r); + } + } + /** + * @depends testPrivateGetEnvironmentContextAlwaysReturnsArrayWithKeys + */ + public function testPrivateGetEnvironmentContextSetsHostByDefault() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $host = '127.0.0.1'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($host, $r['host']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextHonorsExistingHost() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $host = '127.0.0.3'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => $host + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($host, $r['host']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextSetsUserByDefault() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $userInfo = posix_getpwuid(posix_geteuid()); + $user = $userInfo['name']; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($user, $r['user']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextHonorsExistingUser() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $user = 'foo'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => $user, + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($user, $r['user']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + * + */ + public function testPrivateGetEnvironmentContextSetsHashByDefaultIfAvailable() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $hash = $this->createHash(); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($hash, $r['hash']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + * + */ + public function testPrivateGetEnvironmentContextSetsPasswordByDefaultIfAvailable() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $p = 'foo'; + $this->setEnvironmentVars($p); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($p, $r['password']); + $this->assertEmpty($r['hash']); + } + /** + * + */ + public function testInitAdapterRequiresRemoteQueryAsArg() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals('Cpanel_Query_Http_Abstract', $actual); + } + /** + * + */ + public function testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting() + { + $hash = 'blah'; + $password = 'blurg'; + $user = 'foo'; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(0))->method('getOption')->with('host')->will($this->returnValue($host)); + $cs->expects($this->at(1))->method('getOption')->with('user')->will($this->returnValue($user)); + $cs->expects($this->at(2))->method('getOption')->with('hash')->will($this->returnValue($hash)); + $cs->expects($this->at(3))->method('getOption')->with('port')->will($this->returnValue($port)); + $cs->expects($this->at(4))->method('getOption')->with('protocol')->will($this->returnValue($protocol)); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, new concreteCoreRemoteQuery()); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSettingCheckingPasswordAfterHash() + { + $hash = ''; + $password = 'blurg'; + $user = 'foo'; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(0))->method('getOption')->with('host')->will($this->returnValue($host)); + $cs->expects($this->at(1))->method('getOption')->with('user')->will($this->returnValue($user)); + $cs->expects($this->at(2))->method('getOption')->with('hash')->will($this->returnValue($hash)); + $cs->expects($this->at(3))->method('getOption')->with('password')->will($this->returnValue($password)); + $cs->expects($this->at(4))->method('getOption')->with('port')->will($this->returnValue($port)); + $cs->expects($this->at(5))->method('getOption')->with('protocol')->will($this->returnValue($protocol)); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, new concreteCoreRemoteQuery()); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterAttemptsToUsePrivateGetEnvironmentContext() + { + $adapter = new concreteCoreRemoteQuery(); + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $userInfo = posix_getpwuid(posix_geteuid()); + $expectedUser = $userInfo['name']; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(6))->method('getOption')->with('disableEnvironmentContext'); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + $actual = $r->getUser(); + $this->assertEquals($expectedUser, $actual); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterWillNotToUsePrivateGetEnvironmentContextWhenDisabled() + { + $adapter = new concreteCoreRemoteQuery(); + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + $actual = $r->getUser(); + $this->assertEquals($user, $actual); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterCallsAdapterInit() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init' + )); + $adapter->expects($this->once())->method('init')->with($host, $user, $password); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterDoesNotCallAdapterSetPortByDefault() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = ''; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setPort' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->never())->method('setPort'); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetPortWhenPortHasValue() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setPort' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setPort')->with($port); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterDoesNotCallAdapterSetProtocolByDefault() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setProtocol' + )); + //must mock init(), since it will likely call setProtocol() + $adapter->expects($this->any())->method('init'); + //setPort calls setProtocol internally (with a calculated value + $adapter->expects($this->once())->method('setProtocol')->with('http'); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetProtocolWhenPortHasNoValueAndProtocolDoes() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = ''; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setProtocol' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setProtocol')->with($protocol); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetHashWhenHashExists() + { + $hash = 'blah'; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setHash' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setHash')->with($hash); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testIsLocalQueryReturnsFalseByDefault() + { + $cs = $this->getCS(); + $this->assertFalse($cs->isLocalQuery()); + } + public function testIsLocalQueryReturnsTrueWithEnvVar() + { + $this->setLocal(); + $cs = $this->getCS(); + $this->assertTrue($cs->isLocalQuery()); + $this->unsetLocal(); + } + public function testProtectedUpdateResponseObjectAdapterRequiresRQAndOptionalTakesServiceName() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals($this->qa, $actual); + $this->assertEquals('adapterName', $rparams[1]->getName()); + $this->assertTrue($rparams[1]->isDefaultValueAvailable()); + } + public function testProtectedUpdateResponseObjectAdapterReturnsQO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $this->assertInstanceOf($this->qa, $rmeth->invoke($cs, $rObj)); + } + public function testProtectedUpdateResponseObjectAdapterCallValidAdapterOnServiceArg() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->with($aservice)->will($this->returnValue(false)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $this->assertInstanceOf($this->qa, $rmeth->invoke($cs, $rObj, $aservice)); + } + public function testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameOnInvalidService() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->once())->method('validAdapter')->with($aservice)->will($this->returnValue(false)); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj, $aservice); + } + public function testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('validAdapter'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectAdapterStoreAdapterInQO() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($adapterName, $rObj->query->adapter); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillOptimizeForLocalContext() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + $this->assertTrue($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillNotSetOptimizeVarForLocalService() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + $this->assertEmpty($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillNotOptimizeForDisabledLocalService() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName + )); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertNotEquals($oAdapterName, $rObj->query->adapter); + $this->assertEmpty($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterReturnsQO + */ + public function testGenResponesObjectReturnsQO() + { + $cs = $this->getCS(); + $rObj = $cs->genResponseObject(); + $this->assertInstanceOf($this->qa, $rObj); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testGenResponesObjectReturnsQOWithService() + { + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('validAdapter')->will($this->returnValue($adapterName)); + $cs->expects($this->atLeastOnce())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $aservice = $cs->getDefaultAdapterName(); + $rObj = $cs->genResponseObject(); + $this->assertEquals($adapterName, $rObj->query->adapter); + } + public function testGetAdapterRequiresRO() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'getAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals($this->qa, $actual); + } + public function testGetAdapterCallsGetDefaultAdapterNameWhenNoAdapterIsSetInRO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->atLeastOnce())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $a = $cs->getAdapter($rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + } + public function testGetAdapterDoesNotCallGetDefaultAdapterNameWhenAdapterIsSetInRO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $rObj->query->adapter = $oAdapterName; + $cs->expects($this->never())->method('getDefaultAdapterName'); + $a = $cs->getAdapter($rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + } + /** + * @expectedException Exception + */ + public function testGetAdapterThroWhenAdapterIsSetInROThatsDisabled() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $rObj->query->adapter = $oAdapterName; + $cs->expects($this->never())->method('getDefaultAdapterName'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName + )); + $a = $cs->getAdapter($rObj); + } + public function testGetAdapterWillFetchStoredAdapterIfAvailable() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $storeda = new Cpanel_Service_Adapter_WHMapi(); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $storeda + )); + $cs->expects($this->never())->method('getDefaultAdapterName'); + $a = $cs->getAdapter($rObj); + $this->assertEquals(spl_object_hash($storeda), spl_object_hash($a)); + } + public function testGetAdapterWillSpawnNewAdapterAndStoreAReference() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName', + 'spawnAdapter' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('getDefaultAdapterName'); + $cs->expects($this->once())->method('spawnAdapter')->will($this->returnValue($spawneda)); + $a = $cs->getAdapter($rObj); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $storeda = $rprop->getValue($cs); + $this->assertEquals(spl_object_hash($spawneda), spl_object_hash($a)); + $this->assertEquals(spl_object_hash($storeda[$oAdapterName]), spl_object_hash($a)); + } + public function argumentData() + { + return array( + array( + 'string', + 1, + null + ), + array( + new stdClass(), + 1, + null + ), + array( + 1, + 1, + null + ), + array( + array(), + 1, + null + ), + array( + array( + 'one', + 'two' + ), + 0, + 'ordinal' + ), + array( + array( + 'one' => 'one', + 'two' => 'two', + ), + 0, + 'associative' + ), + array( + array( + 'one' => 'one', + 'two', + ), + 0, + 'ordinal' + ), + ); + } + /** + * @dataProvider argumentData + * @paramsunknown_type $input + * @paramsunknown_type $expectExcep + * @paramsunknown_type $expectType + */ + public function testProtectedArrayTypeThrowsOnBadInput($input, $expectExcep, $expectType = null) + { + $cs = $this->getCS(); + $rprop = new ReflectionMethod($cs, 'arrayType'); + $rprop->setAccessible(true); + if ($expectExcep) { + $this->setExpectedException('Exception'); + } + $rprop->invoke($cs, $input); + } + /** + * @dataProvider argumentData + * @paramsunknown_type $input + * @paramsunknown_type $expectExcep + * @paramsunknown_type $expectType + * @depends testClassHasConstants + * @depends testProtectedArrayTypeThrowsOnBadInput + */ + public function testProtectedArrayTypeReturnRightType($input, $expectExcep, $expectType = null) + { + if (!is_null($expectType)) { + $cs = $this->getCS(); + $rprop = new ReflectionMethod($cs, 'arrayType'); + $rprop->setAccessible(true); + $this->assertEquals($expectType, $rprop->invoke($cs, $input)); + } + } + public function testLegacySetUnderscoreTypeUsesStoreAdapter() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $cs->set_output($type); + } + public function testLegacySetUnderscoreTypeAltersAdapter() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $this->assertNotEquals($type, $spawneda->getResponseFormatType()); + $cs->set_output($type); + $this->assertEquals($type, $spawneda->getAdapterResponseFormatType()); + } + public function testLegacySetUnderscoreTypeSpawnsAdapterAsNecessary() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName', + 'getAdapter' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->once())->method('genResponseObject')->will($this->returnValue($rObj)); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $cs->expects($this->once())->method('getAdapter')->will($this->returnValue($spawneda)); + $cs->set_output($type); + } + public function badSetOutputData() + { + return array( + array( + 'blah' + ), + array( + new stdClass() + ), + array( + 1 + ), + array( + 'LiveJSON' + ), + ); + } + /** + * @dataProvider badSetOutputData + * @expectedException Exception + * @depends testLegacySetUnderscoreTypeUsesStoreAdapter + */ + public function testLegacySetUnderscoreTypeThrowOnBadInput($type) + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $cs->set_output($type); + } + public function testProtectedCheckParamsHasRightInputArgs() + { + $args = array( + 'service', + 'mf', + 'args', + 'method', + 'argType' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rparams = $rmeth->getParameters(); + foreach ($rparams as $param) { + $actual[$param->getPosition() ] = $param->getName(); + } + $this->assertEquals($args, $actual); + } + public function checkParamData() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + '', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + ), + array( + 'WHM', + array(), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + ), + ); + } + /** + * @dataProvider checkParamData + * @depends testClassHasConstants + * @depends testProtectedCheckParamsHasRightInputArgs + * @expectedException Exception + */ + public function testProtectedCheckParamsThrowsOnBadInput($service, $mf, $args, $method, $argType) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->never())->method('validAdapter'); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } + public function checkParamData2() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'blah', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + 0, + 0, + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0, + 0, + 0, + ), + ); + } + /** + * @dataProvider checkParamData2 + * @depends testProtectedCheckParamsThrowsOnBadInput + */ + public function testProtectedCheckParamsNormalizesService($service, $mf, $args, $method, $argType, $expectFail, $expectIsLocalQuery, $live) + { + if ($live) { + $this->setLocal(); + } + $cs = $this->getCS(array( + 'validAdapter', + 'isLocalQuery' + )); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $fail = ($expectFail) ? false : $service; + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($fail)); + if ($expectIsLocalQuery) { + $cs->expects($this->once())->method('isLocalQuery')->will($this->returnValue($live)); + } else { + $cs->expects($this->never())->method('isLocalQuery'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + if ($live) { + $this->unsetLocal(); + } + } + public function checkParamData3() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'live', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + 1, + 0, + ), + array( + 'live', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0, + 1, + 'live', + ), + ); + } + /** + * @dataProvider checkParamData3 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsNormalizesLiveService($service, $mf, $args, $method, $argType, $expectFail, $expectIsLocalQuery, $live) + { + if ($live) { + $this->setLocal(); + } + $cs = $this->getCS(array( + 'validAdapter', + 'isLocalQuery' + )); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($service)); + $cs->expects($this->once())->method('isLocalQuery')->will($this->returnValue($live)); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + if ($live) { + $this->unsetLocal(); + } + } + public function checkParamData4() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + '', + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'WHM', + array( + 'module' => 'Email' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + 'string', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + new stdClass(), + 'someMethodNameForLogging', + $api1, + 1 + ), + ); + } + /** + * @dataProvider checkParamData4 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsChecksModuleFunctionArray($service, $mf, $args, $method, $argType, $expectFail) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + if ($service == 'WHM') { + $n = $cs::ADAPTER_WHM; + } else { + $n = $service; + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($n)); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } + public function checkParamData5() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api2, + 1 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one' => 'one', + 'two' => 'two' + ), + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + array( + 'one' => 'one', + 'two' => 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + ), + ); + } + /** + * @dataProvider checkParamData5 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsChecksArrayType($service, $mf, $args, $method, $argType, $expectFail) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + if ($service == 'WHM') { + $n = $cs::ADAPTER_WHM; + } else { + $n = $service; + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($n)); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } +} diff --git a/Cpanel/Tests/Cpanel/Service/AbstractTest.php.bak b/Cpanel/Tests/Cpanel/Service/AbstractTest.php.bak new file mode 100644 index 0000000..e2dfaef --- /dev/null +++ b/Cpanel/Tests/Cpanel/Service/AbstractTest.php.bak @@ -0,0 +1,1725 @@ +getMock($this->cut, $methods, $args, $mockName, $callConst, $callClone, $callA); + return $m; + } + /** + * @param bool $mock Return a PHPUnit mock object + * @param unknown_type $methods + * @param unknown_type $args + * @param unknown_type $mockName + * @param unknown_type $callConst + * @param unknown_type $callClone + * @param unknown_type $callA + * + * + * @return Cpanel_Query_Object + */ + public function getRObj($mock = false, $methods = array(), $args = array(), $mockName = '', $callConst = true, $callClone = true, $callA = true) + { + if ($mock) { + if (empty($methods)) { + $methods = null; + } + return $this->getMock($this->qa, $methods, $args, $mockName, $callConst, $callClone, $callA); + } + return new Cpanel_Query_Object(); + } + + public function tearDown() + { + self::restoreHash(); + } + + public function createHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $hash = "foo\nbar\n"; + $fh = fopen($filename, 'w+'); + fwrite($fh, $hash); + fclose($fh); + return $hash; + } + public function removeHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $filenameBck = $filename."_bck"; + + if (file_exists($filename)) { + if(!rename($filename, $filenameBck)) { + self::fail("Can't create backup file of $filename"); + exit(); + } + } + } + + public function restoreHash() + { + $userInfo = posix_getpwuid(posix_geteuid()); + $filename = $userInfo['dir'] . '/.accesshash'; + $filenameBck = $filename."_bck"; + + if (file_exists($filenameBck)) { + if(!rename($filenameBck, $filename)) { + self::fail("Can't restore file $filename. Please restore it manually from $filenameBck"); + exit(); + } + } + } + + public function setEnvironmentVars($password = false, $server = 'cpsrvd') + { + if ($password) { + putenv('REMOTE_PASSWORD=' . $password); + } elseif (is_null($password)) { + //set to blank + putenv('REMOTE_PASSWORD='); + } elseif ($password === false) { + putenv('REMOTE_PASSWORD=__HIDDEN__'); + } + putenv('SERVER_SOFTWARE=' . $server); + } + public function removeEnvironmentVars() + { + putenv('REMOTE_PASSWORD'); + putenv('SERVER_SOFTWARE'); + } + public function setLocal() + { + $filename = '/tmp/publicapi.test.sock'; + putenv('CPANEL_PHPCONNECT_SOCKET=' . $filename); + touch($filename); + } + public function unsetLocal() + { + $filename = '/tmp/publicapi.test.sock'; + putenv('CPANEL_PHPCONNECT_SOCKET'); + if (file_exists($filename)) { + unlink($filename); + } + } + public function testHasAbstractMethods() + { + $expected = array( + 'getDefaultAdapterName', + 'validAdapter', + 'spawnAdapter', + ); + sort($expected); + $rclass = new ReflectionClass($this->real_cut); + $methods = $rclass->getMethods(); + foreach ($methods as $meth) { + if ($meth->isAbstract()) { + $actual[] = $meth->getName(); + } + } + sort($actual); + $this->assertEquals($expected, $actual); + } + /** + * @expectedException Exception + * @depends testHasAbstractMethods + */ + public function testConstructThrowsOnBadInput() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($this->cut, '__construct'); + $rmeth->invoke($cs, 'fakestring'); + } + public function classConstants() + { + return array( + array( + 'API1ARGS', + 'ordinal' + ), + array( + 'API2ARGS', + 'associative' + ), + array( + 'ADAPTER_CPANEL', + 'cpanel' + ), + array( + 'ADAPTER_WHM', + 'whostmgr' + ), + array( + 'ADAPTER_LIVE', + 'live' + ), + ); + } + /** + * @dataProvider classConstants + * + */ + public function testClassHasConstants($name, $value) + { + $rclass = new ReflectionClass($this->real_cut); + $actual = $rclass->getConstants(); + $this->assertArrayHasKey($name, $actual); + $this->assertEquals($value, $actual[$name]); + } + /** + * + */ + public function testInstantiationSetsALogger() + { + $cs = new $this->cut(); + $this->assertInstanceOf('Cpanel_Listner_Subject_Abstract', $cs->listner); + } + /** + * @expectedException Exception + */ + public function testDisableAdapterThrowOnBadType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue(false)); + $cs->disableAdapter('cpanel'); + } + public function testDisableAdapterStoresOnValidType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue('foo')); + $cs->disableAdapter('cpanel'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $da = $rprop->getValue($cs); + $this->assertTrue(in_array('foo', $da)); + } + /** + * @expectedException Exception + */ + public function testEnableAdapterThrowOnBadType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue(false)); + $cs->enableAdapter('cpanel'); + } + public function testEnableAdapterRemovesOnValidType() + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue('foo')); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + 'foo' + )); + $cs->enableAdapter('cpanel'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $da = $rprop->getValue($cs); + $this->assertFalse(in_array('foo', $da)); + } + public function testPrivategetEUIDAuthReturnsArray() + { + $this->removeHash(); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertInternalType('array', $r); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithParticularKeys() + { + $this->removeHash(); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithHashWhenAvailable() + { + $this->removeHash(); + $hash = $this->createHash(); + $expected = array( + 'hash' => $hash, + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithPasswordWhenAvailable() + { + $this->removeHash(); + $this->setEnvironmentVars('foo'); + $expected = array( + 'hash' => '', + 'password' => 'foo' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenNoPassword() + { + $this->removeHash(); + $this->setEnvironmentVars(null); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenPasswordHidden() + { + $this->removeHash(); + $this->setEnvironmentVars(false); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivategetEUIDAuthReturnArrayWithoutPasswordWhenNotCpsrvd() + { + $this->removeHash(); + $this->setEnvironmentVars('foo', 'webmail'); + $expected = array( + 'hash' => '', + 'password' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEUIDAuth'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($expected, $r); + $this->removeEnvironmentVars(); + } + /** + * @depends testPrivategetEUIDAuthReturnsArray + */ + public function testPrivateGetEnvironmentContextAlwaysReturnsArrayWithKeys() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + foreach ($expected as $key => $value) { + $this->assertArrayHasKey($key, $r); + } + } + /** + * @depends testPrivateGetEnvironmentContextAlwaysReturnsArrayWithKeys + */ + public function testPrivateGetEnvironmentContextSetsHostByDefault() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $host = '127.0.0.1'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs); + $this->assertEquals($host, $r['host']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextHonorsExistingHost() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $host = '127.0.0.3'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => $host + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($host, $r['host']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextSetsUserByDefault() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $userInfo = posix_getpwuid(posix_geteuid()); + $user = $userInfo['name']; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($user, $r['user']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + */ + public function testPrivateGetEnvironmentContextHonorsExistingUser() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $user = 'foo'; + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => $user, + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($user, $r['user']); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + * + */ + public function testPrivateGetEnvironmentContextSetsHashByDefaultIfAvailable() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $hash = $this->createHash(); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($hash, $r['hash']); + //$this->removeHash(); + } + /** + * @depends testPrivateGetEnvironmentContextSetsHostByDefault + * + */ + public function testPrivateGetEnvironmentContextSetsPasswordByDefaultIfAvailable() + { + $this->removeHash(); + $this->removeEnvironmentVars(); + $p = 'foo'; + $this->setEnvironmentVars($p); + $expected = array( + 'hash' => '', + 'password' => '', + 'user' => '', + 'host' => '' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, '_getEnvironmentContext'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $expected); + $this->assertEquals($p, $r['password']); + $this->assertEmpty($r['hash']); + } + /** + * + */ + public function testInitAdapterRequiresRemoteQueryAsArg() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals('Cpanel_Query_Http_Abstract', $actual); + } + /** + * + */ + public function testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting() + { + $hash = 'blah'; + $password = 'blurg'; + $user = 'foo'; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(0))->method('getOption')->with('host')->will($this->returnValue($host)); + $cs->expects($this->at(1))->method('getOption')->with('user')->will($this->returnValue($user)); + $cs->expects($this->at(2))->method('getOption')->with('hash')->will($this->returnValue($hash)); + $cs->expects($this->at(3))->method('getOption')->with('port')->will($this->returnValue($port)); + $cs->expects($this->at(4))->method('getOption')->with('protocol')->will($this->returnValue($protocol)); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, new concreteCoreRemoteQuery()); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSettingCheckingPasswordAfterHash() + { + $hash = ''; + $password = 'blurg'; + $user = 'foo'; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(0))->method('getOption')->with('host')->will($this->returnValue($host)); + $cs->expects($this->at(1))->method('getOption')->with('user')->will($this->returnValue($user)); + $cs->expects($this->at(2))->method('getOption')->with('hash')->will($this->returnValue($hash)); + $cs->expects($this->at(3))->method('getOption')->with('password')->will($this->returnValue($password)); + $cs->expects($this->at(4))->method('getOption')->with('port')->will($this->returnValue($port)); + $cs->expects($this->at(5))->method('getOption')->with('protocol')->will($this->returnValue($protocol)); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, new concreteCoreRemoteQuery()); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterAttemptsToUsePrivateGetEnvironmentContext() + { + $adapter = new concreteCoreRemoteQuery(); + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $userInfo = posix_getpwuid(posix_geteuid()); + $expectedUser = $userInfo['name']; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + ); + $cs = $this->getCS(array( + 'getOption' + ), array( + $expected + )); + $cs->expects($this->at(6))->method('getOption')->with('disableEnvironmentContext'); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + $actual = $r->getUser(); + $this->assertEquals($expectedUser, $actual); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterWillNotToUsePrivateGetEnvironmentContextWhenDisabled() + { + $adapter = new concreteCoreRemoteQuery(); + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + $actual = $r->getUser(); + $this->assertEquals($user, $actual); + } + /** + * @depends testInitAdapterAttemptsToGetVariousEnviromentVarsInternalBeforeSetting + */ + public function testInitAdapterCallsAdapterInit() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init' + )); + $adapter->expects($this->once())->method('init')->with($host, $user, $password); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterDoesNotCallAdapterSetPortByDefault() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = ''; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setPort' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->never())->method('setPort'); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetPortWhenPortHasValue() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setPort' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setPort')->with($port); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterDoesNotCallAdapterSetProtocolByDefault() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setProtocol' + )); + //must mock init(), since it will likely call setProtocol() + $adapter->expects($this->any())->method('init'); + //setPort calls setProtocol internally (with a calculated value + $adapter->expects($this->once())->method('setProtocol')->with('http'); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetProtocolWhenPortHasNoValueAndProtocolDoes() + { + $hash = ''; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = ''; + $protocol = 'http'; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setProtocol' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setProtocol')->with($protocol); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testInitAdapterCallAdapterSetHashWhenHashExists() + { + $hash = 'blah'; + $password = 'blurg'; + $user = ''; + $host = 'bar'; + $port = '2095'; + $protocol = ''; + $expected = array( + 'hash' => $hash, + 'password' => $password, + 'user' => $user, + 'host' => $host, + 'port' => $port, + 'protocol' => $protocol, + 'disableEnvironmentContext' => true, + ); + $adapter = $this->getMock('concreteCoreRemoteQuery', array( + 'init', + 'setHash' + )); + //must mock init(), since it may call setPort() + $adapter->expects($this->any())->method('init'); + $adapter->expects($this->once())->method('setHash')->with($hash); + $cs = $this->getCS(null, array( + $expected + )); + $rmeth = new ReflectionMethod($cs, 'initAdapter'); + $rmeth->setAccessible(true); + $r = $rmeth->invoke($cs, $adapter); + } + public function testIsLocalQueryReturnsFalseByDefault() + { + $cs = $this->getCS(); + $this->assertFalse($cs->isLocalQuery()); + } + public function testIsLocalQueryReturnsTrueWithEnvVar() + { + $this->setLocal(); + $cs = $this->getCS(); + $this->assertTrue($cs->isLocalQuery()); + $this->unsetLocal(); + } + public function testProtectedUpdateResponseObjectAdapterRequiresRQAndOptionalTakesServiceName() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals($this->qa, $actual); + $this->assertEquals('adapterName', $rparams[1]->getName()); + $this->assertTrue($rparams[1]->isDefaultValueAvailable()); + } + public function testProtectedUpdateResponseObjectAdapterReturnsQO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $this->assertInstanceOf($this->qa, $rmeth->invoke($cs, $rObj)); + } + public function testProtectedUpdateResponseObjectAdapterCallValidAdapterOnServiceArg() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->once())->method('validAdapter')->with($aservice)->will($this->returnValue(false)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $this->assertInstanceOf($this->qa, $rmeth->invoke($cs, $rObj, $aservice)); + } + public function testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameOnInvalidService() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->once())->method('validAdapter')->with($aservice)->will($this->returnValue(false)); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj, $aservice); + } + public function testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('validAdapter'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectAdapterStoreAdapterInQO() + { + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($adapterName, $rObj->query->adapter); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillOptimizeForLocalContext() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + // $adapterName = 'Cpanel_Service_Adapter_WHMapi'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + $this->assertTrue($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillNotSetOptimizeVarForLocalService() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + $this->assertEmpty($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testProtectedUpdateResponseObjectWillNotOptimizeForDisabledLocalService() + { + $this->setLocal(); + $rObj = $this->getRObj(); + $aservice = 'blah'; + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->any())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName + )); + $rmeth = new ReflectionMethod($cs, 'updateResponseObjectAdapter'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $rObj); + $this->assertNotEquals($oAdapterName, $rObj->query->adapter); + $this->assertEmpty($rObj->query->optimized); + $this->unsetLocal(); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterReturnsQO + */ + public function testGenResponesObjectReturnsQO() + { + $cs = $this->getCS(); + $rObj = $cs->genResponseObject(); + $this->assertInstanceOf($this->qa, $rObj); + } + /** + * @depends testProtectedUpdateResponseObjectAdapterGetsDefaultAdapterNameByDefault + */ + public function testGenResponesObjectReturnsQOWithService() + { + $adapterName = 'foo'; + $cs = $this->getCS(array( + 'validAdapter', + 'getDefaultAdapterName' + )); + $cs->expects($this->any())->method('validAdapter')->will($this->returnValue($adapterName)); + $cs->expects($this->atLeastOnce())->method('getDefaultAdapterName')->will($this->returnValue($adapterName)); + $aservice = $cs->getDefaultAdapterName(); + $rObj = $cs->genResponseObject(); + $this->assertEquals($adapterName, $rObj->query->adapter); + } + public function testGetAdapterRequiresRO() + { + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'getAdapter'); + $rparams = $rmeth->getParameters(); + $actual = $rparams[0]->getClass()->getName(); + $this->assertEquals($this->qa, $actual); + } + public function testGetAdapterCallsGetDefaultAdapterNameWhenNoAdapterIsSetInRO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $cs->expects($this->atLeastOnce())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $a = $cs->getAdapter($rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + } + public function testGetAdapterDoesNotCallGetDefaultAdapterNameWhenAdapterIsSetInRO() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $rObj->query->adapter = $oAdapterName; + $cs->expects($this->never())->method('getDefaultAdapterName'); + $a = $cs->getAdapter($rObj); + $this->assertEquals($oAdapterName, $rObj->query->adapter); + } + /** + * @expectedException Exception + */ + public function testGetAdapterThroWhenAdapterIsSetInROThatsDisabled() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_LIVE; + $rObj->query->adapter = $oAdapterName; + $cs->expects($this->never())->method('getDefaultAdapterName'); + $rprop = new ReflectionProperty($cs, 'disabledAdapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName + )); + $a = $cs->getAdapter($rObj); + } + public function testGetAdapterWillFetchStoredAdapterIfAvailable() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $storeda = new Cpanel_Service_Adapter_WHMapi(); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $storeda + )); + $cs->expects($this->never())->method('getDefaultAdapterName'); + $a = $cs->getAdapter($rObj); + $this->assertEquals(spl_object_hash($storeda), spl_object_hash($a)); + } + public function testGetAdapterWillSpawnNewAdapterAndStoreAReference() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'getDefaultAdapterName', + 'spawnAdapter' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('getDefaultAdapterName'); + $cs->expects($this->once())->method('spawnAdapter')->will($this->returnValue($spawneda)); + $a = $cs->getAdapter($rObj); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $storeda = $rprop->getValue($cs); + $this->assertEquals(spl_object_hash($spawneda), spl_object_hash($a)); + $this->assertEquals(spl_object_hash($storeda[$oAdapterName]), spl_object_hash($a)); + } + public function argumentData() + { + return array( + array( + 'string', + 1, + null + ), + array( + new stdClass(), + 1, + null + ), + array( + 1, + 1, + null + ), + array( + array(), + 1, + null + ), + array( + array( + 'one', + 'two' + ), + 0, + 'ordinal' + ), + array( + array( + 'one' => 'one', + 'two' => 'two', + ), + 0, + 'associative' + ), + array( + array( + 'one' => 'one', + 'two', + ), + 0, + 'ordinal' + ), + ); + } + /** + * @dataProvider argumentData + * @paramsunknown_type $input + * @paramsunknown_type $expectExcep + * @paramsunknown_type $expectType + */ + public function testProtectedArrayTypeThrowsOnBadInput($input, $expectExcep, $expectType = null) + { + $cs = $this->getCS(); + $rprop = new ReflectionMethod($cs, 'arrayType'); + $rprop->setAccessible(true); + if ($expectExcep) { + $this->setExpectedException('Exception'); + } + $rprop->invoke($cs, $input); + } + /** + * @dataProvider argumentData + * @paramsunknown_type $input + * @paramsunknown_type $expectExcep + * @paramsunknown_type $expectType + * @depends testClassHasConstants + * @depends testProtectedArrayTypeThrowsOnBadInput + */ + public function testProtectedArrayTypeReturnRightType($input, $expectExcep, $expectType = null) + { + if (!is_null($expectType)) { + $cs = $this->getCS(); + $rprop = new ReflectionMethod($cs, 'arrayType'); + $rprop->setAccessible(true); + $this->assertEquals($expectType, $rprop->invoke($cs, $input)); + } + } + public function testLegacySetUnderscoreTypeUsesStoreAdapter() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $cs->set_output($type); + } + public function testLegacySetUnderscoreTypeAltersAdapter() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $this->assertNotEquals($type, $spawneda->getResponseFormatType()); + $cs->set_output($type); + $this->assertEquals($type, $spawneda->getAdapterResponseFormatType()); + } + public function testLegacySetUnderscoreTypeSpawnsAdapterAsNecessary() + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName', + 'getAdapter' + )); + $type = 'XML'; + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->once())->method('genResponseObject')->will($this->returnValue($rObj)); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $cs->expects($this->once())->method('getAdapter')->will($this->returnValue($spawneda)); + $cs->set_output($type); + } + public function badSetOutputData() + { + return array( + array( + 'blah' + ), + array( + new stdClass() + ), + array( + 1 + ), + array( + 'LiveJSON' + ), + ); + } + /** + * @dataProvider badSetOutputData + * @expectedException Exception + * @depends testLegacySetUnderscoreTypeUsesStoreAdapter + */ + public function testLegacySetUnderscoreTypeThrowOnBadInput($type) + { + $rObj = $this->getRObj(); + $cs = $this->getCS(array( + 'genResponseObject', + 'getDefaultAdapterName' + )); + $oAdapterName = $cs::ADAPTER_WHM; + $rObj->query->adapter = $oAdapterName; + $spawneda = new Cpanel_Service_Adapter_WHMapi(); + $cs->expects($this->never())->method('genResponseObject'); + $cs->expects($this->once())->method('getDefaultAdapterName')->will($this->returnValue($oAdapterName)); + $rprop = new ReflectionProperty($cs, 'adapters'); + $rprop->setAccessible(true); + $rprop->setValue($cs, array( + $oAdapterName => $spawneda + )); + $cs->set_output($type); + } + public function testProtectedCheckParamsHasRightInputArgs() + { + $args = array( + 'service', + 'mf', + 'args', + 'method', + 'argType' + ); + $cs = $this->getCS(); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rparams = $rmeth->getParameters(); + foreach ($rparams as $param) { + $actual[$param->getPosition() ] = $param->getName(); + } + $this->assertEquals($args, $actual); + } + public function checkParamData() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + '', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + ), + array( + 'WHM', + array(), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + ), + ); + } + /** + * @dataProvider checkParamData + * @depends testClassHasConstants + * @depends testProtectedCheckParamsHasRightInputArgs + * @expectedException Exception + */ + public function testProtectedCheckParamsThrowsOnBadInput($service, $mf, $args, $method, $argType) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + $cs->expects($this->never())->method('validAdapter'); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } + public function checkParamData2() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'blah', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + 0, + 0, + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0, + 0, + 0, + ), + ); + } + /** + * @dataProvider checkParamData2 + * @depends testProtectedCheckParamsThrowsOnBadInput + */ + public function testProtectedCheckParamsNormalizesService($service, $mf, $args, $method, $argType, $expectFail, $expectIsLocalQuery, $live) + { + if ($live) { + $this->setLocal(); + } + $cs = $this->getCS(array( + 'validAdapter', + 'isLocalQuery' + )); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $fail = ($expectFail) ? false : $service; + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($fail)); + if ($expectIsLocalQuery) { + $cs->expects($this->once())->method('isLocalQuery')->will($this->returnValue($live)); + } else { + $cs->expects($this->never())->method('isLocalQuery'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + if ($live) { + $this->unsetLocal(); + } + } + public function checkParamData3() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'live', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + 1, + 0, + ), + array( + 'live', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0, + 1, + 'live', + ), + ); + } + /** + * @dataProvider checkParamData3 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsNormalizesLiveService($service, $mf, $args, $method, $argType, $expectFail, $expectIsLocalQuery, $live) + { + if ($live) { + $this->setLocal(); + } + $cs = $this->getCS(array( + 'validAdapter', + 'isLocalQuery' + )); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($service)); + $cs->expects($this->once())->method('isLocalQuery')->will($this->returnValue($live)); + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + if ($live) { + $this->unsetLocal(); + } + } + public function checkParamData4() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + '', + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'WHM', + array( + 'module' => 'Email' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email' + ), + '', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + 'string', + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + new stdClass(), + 'someMethodNameForLogging', + $api1, + 1 + ), + ); + } + /** + * @dataProvider checkParamData4 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsChecksModuleFunctionArray($service, $mf, $args, $method, $argType, $expectFail) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + if ($service == 'WHM') { + $n = $cs::ADAPTER_WHM; + } else { + $n = $service; + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($n)); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } + public function checkParamData5() + { + $api1 = 'ordinal'; + $api2 = 'associative'; + return array( + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api2, + 1 + ), + array( + 'WHM', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk', + 'user' => 'foo' + ), + array( + 'one' => 'one', + 'two' => 'two' + ), + 'someMethodNameForLogging', + $api1, + 1 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + array( + 'one', + 'two' + ), + 'someMethodNameForLogging', + $api1, + 0 + ), + array( + 'cpanel', + array( + 'module' => 'Email', + 'function' => 'listpopswithdisk' + ), + array( + 'one' => 'one', + 'two' => 'two' + ), + 'someMethodNameForLogging', + $api1, + 1, + ), + ); + } + /** + * @dataProvider checkParamData5 + * @depends testProtectedCheckParamsNormalizesService + */ + public function testProtectedCheckParamsChecksArrayType($service, $mf, $args, $method, $argType, $expectFail) + { + $cs = $this->getCS(array( + 'validAdapter' + )); + if ($service == 'WHM') { + $n = $cs::ADAPTER_WHM; + } else { + $n = $service; + } + $cs->expects($this->once())->method('validAdapter')->will($this->returnValue($n)); + if ($expectFail) { + $this->setExpectedException('Exception'); + } + $rmeth = new ReflectionMethod($cs, 'checkParams'); + $rmeth->setAccessible(true); + $rmeth->invoke($cs, $service, $mf, $args, $method, $argType); + } +} diff --git a/Cpanel/Tests/Cpanel/Service/Adapter/LiveapiTest.php b/Cpanel/Tests/Cpanel/Service/Adapter/LiveapiTest.php index dddab40..af695b8 100644 --- a/Cpanel/Tests/Cpanel/Service/Adapter/LiveapiTest.php +++ b/Cpanel/Tests/Cpanel/Service/Adapter/LiveapiTest.php @@ -82,7 +82,10 @@ public static function startMSS() if (!file_exists($mockserverscript)) { self::fail("Mock socket server script '$mockserverscript' does not exist"); } - $cmd = "/usr/bin/php -f $mockserverscript"; + + $phpPath = exec('which php'); + $cmd = "$phpPath -f $mockserverscript"; //original + $arg = "socketfile={$socketfile}"; $full_cmd = "nohup $cmd $arg > /dev/null 2>&1 & echo $!"; // > /dev/null $PID = exec($full_cmd); diff --git a/Cpanel/Tests/MockSocketServer.php b/Cpanel/Tests/MockSocketServer.php index 019536e..66e498f 100644 --- a/Cpanel/Tests/MockSocketServer.php +++ b/Cpanel/Tests/MockSocketServer.php @@ -31,10 +31,11 @@ public function listen() $this->log("Start accepting connections: " . $this->socketfile . "\n"); $tmpsocket = socket_accept($this->_socket); if ($tmpsocket === false) { - return $this->throwSocketError($socket, "Socket accept failed"); + return $this->throwSocketError($this->socket, "Socket accept failed"); } $this->log("Accepting connections.\n"); $this->_tmpsocket = $tmpsocket; + // Wait for data on wire while (true) { // Sit on read until we get a newline, anything before that should be a char length @@ -90,6 +91,7 @@ private function setUpSocket() { //open socket $socket = socket_create(AF_UNIX, SOCK_STREAM, 0); + if (!$socket) { return $this->throwSocketError($socket, "Socket could not be created"); }