diff --git a/.gitignore b/.gitignore index a17df00..4fa40ca 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ parseConfig.php +/nbproject/private/ \ No newline at end of file diff --git a/EnhanceTestFramework.php b/EnhanceTestFramework.php index d83cca6..d5f1b21 100644 --- a/EnhanceTestFramework.php +++ b/EnhanceTestFramework.php @@ -325,6 +325,34 @@ class TextEn public $LineFile = 'Line {0} in file {1}'; } +class TextFr +{ + public $FormatForTestRunTook = 'Le test a prit {0} seconds'; + public $FormatForExpectedButWas = '{0} attendu mais finalement fut {1}'; + public $FormatForExpectedNotButWas = 'Pas {0} attendu mais finalement fut {1}'; + public $FormatForExpectedContainsButWas = 'Présence de {0} attendu mais finalement fut {1}'; + public $FormatForExpectedNotContainsButWas = 'Absence de {0} attendu mais finalement fut {1}'; + public $EnhanceTestFramework = 'Enhance Test Framework'; + public $EnhanceTestFrameworkFull = 'Enhance PHP Framework de Test Unitaire'; + public $TestResults = 'Résultats des tests '; + public $Test = 'Test'; + public $TestPassed = 'Test Réussi'; + public $TestFailed = 'Test Échoué'; + public $Passed = 'Réussi'; + public $Failed = 'Échoué'; + public $ExpectationFailed = 'Les attentes ne furent pas atteintes'; + public $Expected = 'Attendu'; + public $Called = 'Appelé '; + public $InconclusiveOrNotImplemented = 'non conclusif ou pas implémenté'; + public $Times = 'fois'; + public $MethodCoverage = 'Couverture des fonctions'; + public $Copyright = 'Copyright'; + public $ExpectedExceptionNotThrown = 'L\'exception attendu ne fut pas levée'; + public $CannotCallVerifyOnStub = 'impossible d\'appeler VerifyExpectations sur la souche'; + public $ReturnsOrThrowsNotBoth = 'Vous ne pouvez définire qu\une seul valeur de retour(1 returns() ou 1 throws())'; + public $ScenarioWithExpectMismatch = 'Les scenarios doivent être initialisé avec le même nombre d\'appel à "with" et "expect"'; + public $LineFile = 'Ligne {0} dans le fichier {1}'; +} class TextDe { public $FormatForTestRunTook = 'Fertig nach {0} Sekunden'; @@ -411,8 +439,36 @@ class TextSp public $CannotCallVerifyOnStub = 'No se puede llamar VerifyExpectations en un stub'; public $ReturnsOrThrowsNotBoth = 'Debe proporcionar un solo valor de retorno (1 returns() ó 1 throws())'; public $ScenarioWithExpectMismatch = 'Escenario debe ser inicializado con el mismo número de llamadas "with" y "expect" '; - public $LineFile = 'Linha {0} no arquivo {1}'; - public $TypeOfVar=" Tipo: "; + public $LineFile = 'Linha {0} no arquivo {1}'; + public $TypeOfVar=" Tipo: "; +} + +class TextRo{ + public $FormatForTestRunTook = 'Testul a durat {0} secunde'; + public $FormatForExpectedButWas = 'Se aștepta {0} dar a returnat {1}'; + public $FormatForExpectedNotButWas = 'Nu se aștepta {0} dar a returnat {1}'; + public $FormatForExpectedContainsButWas = 'Se aștepta sa conțină {0} dar conține {1}'; + public $FormatForExpectedNotContainsButWas = 'Nu se aștepta sa conțină {0} dar conține {1}'; + public $EnhanceTestFramework = 'Enhance Test Framework'; + public $EnhanceTestFrameworkFull = 'Enhance PHP Unit Testing Framework'; + public $TestResults = 'Rezultate Teste'; + public $Test = 'Test'; + public $TestPassed = 'Testul a fost finalizat cu succes.'; + public $TestFailed = 'Testul a eșuat.'; + public $Passed = 'Succes'; + public $Failed = 'Eșuat.'; + public $ExpectationFailed = 'Așteptare eșuată'; + public $Expected = 'Așteptare'; + public $Called = 'Apelat'; + public $InconclusiveOrNotImplemented = 'Neconcludent sau neimplementat'; + public $Times = 'Ori'; + public $MethodCoverage = 'Învăluire metoda'; + public $Copyright = 'Drepturi de autor'; + public $ExpectedExceptionNotThrown = 'Se aștepta o excepție dar nu a fost afișata'; + public $CannotCallVerifyOnStub = 'Nu a putut fi apelata metoda VerifyExpectations pe un stub'; + public $ReturnsOrThrowsNotBoth = 'Trebuie sa alegi numai o valoare pentru a fi returnata (1 returns() sau 1 throws())'; + public $ScenarioWithExpectMismatch = 'Scenariul trebuie inițializat cu același număr de apelări la metoda "with" sau "expect" '; + public $LineFile = 'Linia {0} in fișierul {1}'; } class EnhanceTestFramework @@ -1730,14 +1786,16 @@ class TemplateType class Language { + const French = "Fr"; const English = 'En'; const Deutsch = 'De'; const BrazilianPortuguese = 'PtBr'; - const Spanish = 'Sp'; + const Spanish = 'Sp'; + const Romana = 'Ro'; } class Localisation { public $Language = Language::English; } -?> \ No newline at end of file +?> diff --git a/parse.php b/parse.php index 554d687..6bfc3b2 100644 --- a/parse.php +++ b/parse.php @@ -1,5 +1,4 @@ _appid = $parseConfig::APPID; - $this->_masterkey = $parseConfig::MASTERKEY; - $this->_restkey = $parseConfig::RESTKEY; - $this->_parseurl = $parseConfig::PARSEURL; + $this->_masterkey = $parseConfig::MASTERKEY; + $this->_restkey = $parseConfig::RESTKEY; + $this->_parseurl = $parseConfig::PARSEURL; if(empty($this->_appid) || empty($this->_restkey) || empty($this->_masterkey)){ $this->throwError('You must set your Application ID, Master Key and REST API Key'); @@ -144,6 +144,12 @@ public function dataType($type,$params){ "objectId" => $params[1] ); break; + case 'relatedTo': + $return = array( + "object" => $params[0], // pointer + "key" => $params[1] // key + ); + break; case 'geopoint': $return = array( "__type" => "GeoPoint", diff --git a/parseACL.php b/parseACL.php index cdf7ed5..bbe8802 100644 --- a/parseACL.php +++ b/parseACL.php @@ -10,7 +10,7 @@ $acl->setReadAccessForId('user_id',true); $acl->setWriteAccessForRole('role_name',true); -$object->ACL($acl); +$object->ACL($acl->acl); $object->save(); */ class parseACL{ @@ -20,14 +20,13 @@ public function __construct(){ } private function setAccessForKey($access,$key,$bool){ if(!($access == 'read' || $access == 'write')) return; - if(is_object($this->acl)) $this->acl = array(); - if($bool) $this->acl[$key][$access] = true; + if($bool) $this->acl->$key->$access = true; else { - if(isset($this->acl[$key])){ - unset($this->acl[$key][$access]); - if(sizeof($this->acl[$key]) == 0) unset($this->acl[$key]); + if(isset($this->acl->$key)){ + unset($this->acl->$key->$access); + if(sizeof((array)$this->acl->$key) == 0) unset($this->acl->$key); } - if(sizeof($this->acl) == 0) $this->acl = new stdClass(); + if(sizeof((array)$this->acl) == 0) $this->acl = new stdClass(); } } public function setPublicReadAccess($bool){ diff --git a/parseObject.php b/parseObject.php index b6b8f39..65f9cfe 100644 --- a/parseObject.php +++ b/parseObject.php @@ -2,7 +2,7 @@ class parseObject extends parseRestClient{ public $_includes = array(); - private $_className = ''; + protected $_className = ''; public function __construct($class=''){ if($class != ''){ @@ -21,6 +21,27 @@ public function __set($name,$value){ } } + public function setClassName($className) { + $this->_className = $className; + } + + public function ACL($acl = null) { + if($acl) + $this->data['ACL'] = $acl; + + return $this->data['ACL']; + } + + public function setProperty($name, $value) { + if($name != '_className'){ + $this->data[$name] = $value; + } + } + + public function getProperty($name) { + return $this->data[$name]; + } + public function save(){ if(count($this->data) > 0 && $this->_className != ''){ $request = $this->request(array( @@ -78,7 +99,7 @@ public function delete($id){ return $request; } } - + public function addInclude($name){ $this->_includes[] = $name; } diff --git a/parseQuery.php b/parseQuery.php index cf4cda7..3b008e9 100644 --- a/parseQuery.php +++ b/parseQuery.php @@ -9,7 +9,7 @@ class parseQuery extends parseRestClient{ private $_include = array(); public function __construct($class=''){ - if($class == 'users' || $class == 'installation'){ + if($class == 'users' || $class == 'roles' || $class == 'installation'){ $this->_requestUrl = $class; } elseif($class != ''){ @@ -31,7 +31,6 @@ public function find(){ )); return $request; - } else{ $urlParams = array( @@ -62,6 +61,7 @@ public function find(){ return $request; } } + //setting this to 1 by default since you'd typically only call this function if you were wanting to turn it on public function setCount($bool=1){ if(is_bool($bool)){ @@ -161,6 +161,30 @@ public function whereGreaterThan($key,$value){ } + public function whereGreaterThanDate($key,$date){ + if(isset($key) && isset($date)){ + $this->_query[$key] = array( + '$gt' => $this->dataType('date', $date) + ); + } + else{ + $this->throwError('the $key and $value parameters must be set when setting a "where" query method'); + } + + } + + public function whereLessThanDate($key,$date){ + if(isset($key) && isset($date)){ + $this->_query[$key] = array( + '$lt' => $this->dataType('date', $date) + ); + } + else{ + $this->throwError('the $key and $value parameters must be set when setting a "where" query method'); + } + + } + public function whereLessThan($key,$value){ if(isset($key) && isset($value)){ $this->_query[$key] = array( @@ -290,7 +314,28 @@ public function wherePointer($key,$className,$objectId){ } } - + + /** + * Example - to find users with a particular role id + * $query->whereRelatedTo('users', '_Role', $roleId); + * + * @param type $key + * @param type $className + * @param type $objectId + */ + public function whereRelatedTo($key,$className,$objectId) { + if(isset($key) && isset($className) && isset($objectId)){ + if($className === 'Role') + $className = '_Role'; + if($className === 'User') + $className = '_User'; + $pointer = $this->dataType('pointer', array($className, $objectId)); + $this->_query['$relatedTo'] = $this->dataType('relatedTo', array($pointer, $key)); + } else { + $this->throwError('the $key and $classname and $objectId parameters must be set when setting a "whereRelatedTo" query method'); + } + } + public function whereInQuery($key,$className,$inQuery){ if(isset($key) && isset($className)){ $this->_query[$key] = array( diff --git a/parseRole.php b/parseRole.php new file mode 100644 index 0000000..327ad6f --- /dev/null +++ b/parseRole.php @@ -0,0 +1,221 @@ +data['name'] = $name; + } else { + $this->throwError('include the roleName when creating a parseRole'); + } + } + + /** + * Cannot change name of a role. + * + * @param type $name + * @param type $value + */ + public function __set($name,$value){ + if($name != '_className' && $name != 'name'){ + $this->data[$name] = $value; + } + } + + /* + $parseObject->users = array( + '__op' => 'AddRelation', + 'objects' => array( + $parseObject->dataType('pointer', array('post','8TOXdXf3tz')) + $parseObject->dataType('pointer', array('post','Ed1nuqPvc')), + ) + ); + */ + + /** + * https://parse.com/docs/rest#roles-updating + * @param type $userIds + */ + public function addUsersToRole($userIds = array()) { + $objects = array(); + foreach ($userIds as $userId) { + $objects[] = $this->dataType('pointer', array('_User', $userId)); + } + $this->setProperty('users', array( + '__op' => 'AddRelation', + 'objects' => $objects + )); + } + + /** + * https://parse.com/docs/rest#roles-updating + * @param type $userIds + */ + public function removeUsersFromRole($userIds = array()) { + $objects = array(); + foreach ($userIds as $userId) { + $objects[] = $this->dataType('pointer', array('_User', $userId)); + } + $this->setProperty('users', array( + '__op' => 'RemoveRelation', + 'objects' => $objects + )); + } + + /** + * https://parse.com/docs/rest#roles-creating + * + * @return type + */ + public function save(){ + if(!$this->data['ACL']) + $this->throwError('Must have ACL with role'); + if(count($this->data) > 0 && $this->_className != ''){ + $request = $this->request(array( + 'method' => 'POST', + 'requestUrl' => $this->_className, + 'data' => $this->data, + )); + return $request; + } + } + + /** + * https://parse.com/docs/rest#roles-retrieving + * @param type $id + * @return type + */ + public function get($id){ + if($this->_className != '' || !empty($id)){ + $request = $this->request(array( + 'method' => 'GET', + 'requestUrl' => $this->_className.'/'.$id + )); + + if(!empty($this->_includes)){ + $request['include'] = implode(',', $this->_includes); + } + + return $request; + } + } + + /** + * https://parse.com/docs/rest#roles-updating + * @param type $id + * @return type + */ + public function update($id){ + if($this->_className != '' || !empty($id)){ + $request = $this->request(array( + 'method' => 'PUT', + 'requestUrl' => $this->_className.'/'.$id, + 'data' => $this->data, + )); + + return $request; + } + } + + /** + * https://parse.com/docs/rest#roles-deleting + * @param type $id + * @return type + */ + public function delete($id){ + if($this->_className != '' || !empty($id)){ + $request = $this->request(array( + 'method' => 'DELETE', + 'requestUrl' => $this->_className.'/'.$id + )); + + return $request; + } + } + + /** + * + * @param type $field + * @param type $amount + */ + public function increment($field,$amount){ + $this->throwError("function increment makes no sense for role."); + } + + /** + * + * @param type $id + */ + public function decrement($id){ + $this->throwError("function decrement makes no sense for role."); + } + + + /** + * curl -X POST \ + -H "X-Parse-Application-Id: Z0GKPdAmkIZsKg9nGgSGU52DQBUP2xavHmACYsAI" \ + -H "X-Parse-REST-API-Key: lOQHg18qhxveTkNA6YGIWDtwBpwQ9ULyK2ZwPqTh" \ + -H "Content-Type: application/json" \ + -d '{ + "name": "Moderators", + "ACL": { + "*": { + "read": true + } + } + }' \ + https://api.parse.com/1/roles + */ + + /** + * curl -X POST \ + -H "X-Parse-Application-Id: Z0GKPdAmkIZsKg9nGgSGU52DQBUP2xavHmACYsAI" \ + -H "X-Parse-REST-API-Key: lOQHg18qhxveTkNA6YGIWDtwBpwQ9ULyK2ZwPqTh" \ + -H "Content-Type: application/json" \ + -d '{"score":1337,"playerName":"Sean Plott","cheatMode":false}' \ + https://api.parse.com/1/classes/GameScore + * + */ + + /** + * curl -X PUT \ + -H "X-Parse-Application-Id: Z0GKPdAmkIZsKg9nGgSGU52DQBUP2xavHmACYsAI" \ + -H "X-Parse-REST-API-Key: lOQHg18qhxveTkNA6YGIWDtwBpwQ9ULyK2ZwPqTh" \ + -H "Content-Type: application/json" \ + -d '{"opponents":{"__op":"AddRelation","objects":[{"__type":"Pointer","className":"Player","objectId":"Vx4nudeWn"}]}}' \ + https://api.parse.com/1/classes/GameScore/Ed1nuqPvcm + * + * curl -X PUT \ + -H "X-Parse-Application-Id: Z0GKPdAmkIZsKg9nGgSGU52DQBUP2xavHmACYsAI" \ + -H "X-Parse-Master-Key: F3ywLEYgKT9JzxQKCX6L2UG7Ni6Aq7oIOlZWxVf0" \ + -H "Content-Type: application/json" \ + -d '{ + "users": { + "__op": "AddRelation", + "objects": [ + { + "__type": "Pointer", + "className": "_User", + "objectId": "8TOXdXf3tz" + }, + { + "__type": "Pointer", + "className": "_User", + "objectId": "g7y9tkhB7O" + } + ] + } + }' \ + https://api.parse.com/1/roles/mrmBZvsErB + */ +} + +?> diff --git a/parseUser.php b/parseUser.php index 6e3eda8..539e2c5 100644 --- a/parseUser.php +++ b/parseUser.php @@ -1,7 +1,7 @@ authData)){ - $request = $this->request( array( - 'method' => 'POST', - 'requestUrl' => 'users', - 'data' => array( - 'authData' => $this->authData - ) - )); - return $request; - } - else{ - $this->throwError('authArray must be set use addAuthData method'); + public function socialLogin(){ + if(!empty($this->authData)){ + $request = $this->request( array( + 'method' => 'POST', + 'requestUrl' => 'users', + 'data' => array( + 'authData' => $this->authData + ) + )); + return $request; + } + else{ + $this->throwError('authArray must be set use addAuthData method'); + } } -} public function get($objectId){ + if(!empty($this->_includes)){ + $request['include'] = implode(',', $this->_includes); + } + if($objectId != ''){ $request = $this->request(array( 'method' => 'GET', @@ -163,9 +167,20 @@ public function unlinkAccount($objectId,$sessionToken,$type){ } + public function addInclude($name){ + $this->_includes[] = $name; + } + + public function ACL($acl = null) { + if($acl) + $this->data['ACL'] = $acl; + + return $this->data['ACL']; + } + public function requestPasswordReset($email){ if(!empty($email)){ - $this->email - $email; + $this->email = $email; $request = $this->request(array( 'method' => 'POST', 'requestUrl' => 'requestPasswordReset', @@ -179,9 +194,6 @@ public function requestPasswordReset($email){ $this->throwError('email is required for the requestPasswordReset method'); } + } } - - -} - -?> \ No newline at end of file +?> diff --git a/tests/parseRoleTest.php b/tests/parseRoleTest.php new file mode 100644 index 0000000..6a1f8ed --- /dev/null +++ b/tests/parseRoleTest.php @@ -0,0 +1,100 @@ +parseRole = \Enhance\Core::getCodeCoverageWrapper('parseRole', array('TestRole')); + $this->parseUser = new parseUser(); + $this->testUser = array( + 'username' => 'testUser'.rand(), + 'password' => 'testPass', + 'email' => 'testUser@parse.com', + 'customField' => 'customValue' + ); + + } + + public function testCreateRole(){ + $parseRole = $this->parseRole; + $parseACL = new parseACL(); + $parseACL->setPublicReadAccess(true); + $parseACL->setPublicWriteAccess(false); + $parseACL->setWriteAccessForRole('Administrator', true); + + $parseRole->ACL( $parseACL->acl ); + try { + $return = $parseRole->save(); + } catch (ParseLibraryException $e) { + throw $e; + } + \Enhance\Assert::isTrue( is_object($return) ); + \Enhance\Assert::isNotNull($return->objectId); + $parseRole->setProperty('objectId', $return->objectId); + + $parseRole2 = $parseRole; + $return = $parseRole2->get($parseRole2->getProperty('objectId')); + \Enhance\Assert::isObject($return); + + $return = $parseRole2->delete($parseRole2->getProperty('objectId')); + } + + public function testAddAndRemoveRoleForUser() { + $parseUser = $this->parseUser; + $retUser = $parseUser->signup($this->testUser['username'], $this->testUser['password']); + + // Recreate role + $parseRole = $this->parseRole; + $parseACL = new parseACL(); + $parseACL->setPublicReadAccess(true); + $parseACL->setPublicWriteAccess(false); + $parseACL->setWriteAccessForRole('Administrator', true); + + $parseRole->ACL( $parseACL->acl ); + try { + $return = $parseRole->save(); + $objectId = $return->objectId; + } catch (ParseLibraryException $e) { + throw $e; + } + + $parseRole2 = new parseRole('TestRole'); + $parseRole2->addUsersToRole(array($retUser->objectId)); + $return = $parseRole2->update($objectId); + \Enhance\Assert::isObject($return); + \Enhance\Assert::isNotNull($return->updatedAt); + + // Query parse role + $query = new parseQuery('users'); + $query->whereRelatedTo('users', 'Role', $objectId); + $ret = $query->find(); + \Enhance\Assert::isArray($ret); + \Enhance\Assert::isTrue(count($ret) == 1); + + $parseRole2->removeUsersFromRole(array($retUser->objectId)); + $return2 = $parseRole2->update($objectId); + \Enhance\Assert::isObject($return2); + \Enhance\Assert::isNotNull($return2->updatedAt); + + $parseRole2->delete($objectId); + } +} + +// Run the tests +\Enhance\Core::runTests(); + +?>