Skip to content

Commit

Permalink
Fix test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Frischmuth committed Jul 10, 2012
1 parent 3cc0dd4 commit ccc951c
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 98 deletions.
4 changes: 2 additions & 2 deletions library/Erfurt/Ac/Default.php
Expand Up @@ -397,8 +397,8 @@ public function setDefaultUserRights(array $defaultUserRights)
public function setStore(Erfurt_Store $store)
{
// Check for AC model!
$acModel = $store->getModel($this->_config['acGraphUri'], false);
if (!$acModel) {
$acModelAvailable = $store->isModelAvailable($this->_config['acGraphUri'], false);
if (!$acModelAvailable) {
require_once 'Erfurt/Ac/Exception.php';
throw new Erfurt_Ac_Exception('AC model not available with URI: ' . $this->_config['acGraphUri']);
}
Expand Down
15 changes: 12 additions & 3 deletions library/Erfurt/App.php
Expand Up @@ -505,14 +505,18 @@ public function getAc()
$type = strtolower($config->ac->type);
if ($type === 'rdf') {
require_once 'Erfurt/Ac/Default.php';

$acConfig = array();
if (isset($config->ac->allowDbUser)) {
$acConfig['allowDbUser'] = (bool)$config->ac->allowDbUser;
}

$this->_ac = new Erfurt_Ac_Default($acConfig);
$this->_ac->setUser($this->getAuth()->getIdentity());

if ($this->getAuth()->hasIdentity()) {
$this->_ac->setUser($this->getAuth()->getIdentity());
}

$this->_ac->setStore($this->getStore());
} else if ($type === 'none') {
require_once 'Erfurt/Ac/None.php';
Expand Down Expand Up @@ -570,6 +574,11 @@ public function getAuth()
return $this->_auth;
}

public function setAuth(Erfurt_Auth $auth)
{
$this->_auth = $auth;
}

/**
* Returns a caching instance.
*
Expand Down
182 changes: 90 additions & 92 deletions library/Erfurt/WebId/Consumer/Storage/Store.php
@@ -1,6 +1,4 @@
<?php
require_once 'Erfurt/WebId/Consumer/Storage.php';

class Erfurt_WebId_Consumer_Storage_Store extends Erfurt_WebId_Consumer_Storage
{
private $_config = array(
Expand All @@ -20,121 +18,121 @@ class Erfurt_WebId_Consumer_Storage_Store extends Erfurt_WebId_Consumer_Storage
public function __construct(Erfurt_Store $store, array $config = array())
{
$this->_config = array_merge($this->_config, $config);

$this->_store = $store;
}

public function addUserWithProfile(Erfurt_WebId_Profile $profile)
{
$webId = $profile->webId();
$newUserUri = $this->_config['userBaseUri'] . md5($webId);
$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];
$userMailProp = $this->_config['userMailProp'];
$userNameProp = $this->_config['userNameProp'];
$userAvatarProp = $this->_config['userAvatarProp'];
$addArray = array($newUserUri => array(
EF_RDF_TYPE => array(array(
'type' => 'uri',
'value' => $userClass
)),
$accountOfProp => array(array(
'type' => 'uri',
'value' => $webId
))
));
$mboxValues = $profile->mboxValues();
if (count($mboxValues) > 0) {
$addArray[$newUserUri][$userMailProp] = array(array(
'type' => 'uri',
'value' => $mboxValues[0] // We always use the first mbox only
));
}
$nameValues = $profile->nameValues();
if (count($nameValues) > 0) {
$addArray[$newUserUri][$userNameProp] = array(array(
'type' => 'literal',
'value' => $nameValues[0] // We always use the first name only
));
}
$depictionValues = $profile->depictionValues();
if (count($depictionValues) > 0) {
$addArray[$newUserUri][$userAvatarProp] = array(array(
'type' => 'uri',
'value' => $depictionValues[0] // We always use the first depiction only
));
} else if ($mboxValues > 0) {
$gravatarBase = 'https://secure.gravatar.com/';
$mailHash = md5(strtolower(trim(substr($mboxValues[0], 7))));
$url = $gravatarBase . $mailHash . '?s=80&d=mm';
$addArray[$newUserUri][$userAvatarProp] = array(array(
'type' => 'uri',
'value' => $url
));
}
try {
$this->_store->addMultipleStatements($userGraphUri, $addArray, false);
} catch (Erfurt_Store_Exception $e) {
return false;
}
return true;
$webId = $profile->webId();

$newUserUri = $this->_config['userBaseUri'] . md5($webId);

$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];
$userMailProp = $this->_config['userMailProp'];
$userNameProp = $this->_config['userNameProp'];
$userAvatarProp = $this->_config['userAvatarProp'];

$addArray = array($newUserUri => array(
EF_RDF_TYPE => array(array(
'type' => 'uri',
'value' => $userClass
)),
$accountOfProp => array(array(
'type' => 'uri',
'value' => $webId
))
));

$mboxValues = $profile->mboxValues();
if (count($mboxValues) > 0) {
$addArray[$newUserUri][$userMailProp] = array(array(
'type' => 'uri',
'value' => $mboxValues[0] // We always use the first mbox only
));
}

$nameValues = $profile->nameValues();
if (count($nameValues) > 0) {
$addArray[$newUserUri][$userNameProp] = array(array(
'type' => 'literal',
'value' => $nameValues[0] // We always use the first name only
));
}

$depictionValues = $profile->depictionValues();
if (count($depictionValues) > 0) {
$addArray[$newUserUri][$userAvatarProp] = array(array(
'type' => 'uri',
'value' => $depictionValues[0] // We always use the first depiction only
));
} else if ($mboxValues > 0) {
$gravatarBase = 'https://secure.gravatar.com/';
$mailHash = md5(strtolower(trim(substr($mboxValues[0], 7))));
$url = $gravatarBase . $mailHash . '?s=80&d=mm';

$addArray[$newUserUri][$userAvatarProp] = array(array(
'type' => 'uri',
'value' => $url
));
}

try {
$this->_store->addMultipleStatements($userGraphUri, $addArray, false);
} catch (Erfurt_Store_Exception $e) {
return false;
}

return true;
}

public function hasUserWithProfile(Erfurt_WebId_Profile $profile)
{
$webId = $profile->webId();
$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];
$webId = $profile->webId();

$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];

$sparqlQuery = <<<EOF
ASK FROM <$userGraphUri>
WHERE {
?user a <$userClass> .
?user a <$userClass> .
?user <$accountOfProp> <$webId> .
}
EOF;
return $this->_store->sparqlAsk($sparqlQuery, array('use_ac' => false));
EOF;

return $this->_store->sparqlAsk($sparqlQuery, array('use_ac' => false));
}

public function userUriForProfile(Erfurt_WebId_Profile $profile)
{
$webId = $profile->webId();
$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];

$userGraphUri = $this->_config['userGraphUri'];
$userClass = $this->_config['userClass'];
$accountOfProp = $this->_config['userAccountOfProp'];

$sparqlQuery = <<<EOF
SELECT ?user FROM <$userGraphUri>
WHERE {
?user a <$userClass> .
?user a <$userClass> .
?user <$accountOfProp> <$webId> .
}
LIMIT 1
EOF;
$retVal = null;
$result = $this->_store->sparqlQuery($sparqlQuery, array(
EOF;

$retVal = null;
$result = $this->_store->sparqlQuery($sparqlQuery, array(
'use_ac' => false,
STORE_RESULTFORMAT => STORE_RESULTFORMAT_PLAIN
));
if (count($result) === 1) {
if (count($result) === 1) {
$retVal = $result[0]['user'];
}
return $retVal;
}

return $retVal;
}
}
10 changes: 10 additions & 0 deletions tests/Erfurt/Ac/DefaultTest.php
Expand Up @@ -1147,4 +1147,14 @@ public function testAddUserModelRuleWithStoreDenyEdit()
$result = $this->_object->addUserModelRule($modelUri, Erfurt_Ac::ACCESS_TYPE_EDIT, Erfurt_Ac::ACCESS_PERM_DENY);
$this->assertTrue($result);
}

/**
* @expectedException Erfurt_Ac_Exception
*/
public function testSetStoreAcGraphNotAvailable()
{
$storeAdapter = new Erfurt_Store_Adapter_Test();
$store = new Erfurt_Store(array('adapterInstance' => $storeAdapter), 'test');
$this->_object->setStore($store);
}
}
14 changes: 13 additions & 1 deletion tests/Erfurt/AppTest.php
Expand Up @@ -326,8 +326,20 @@ public function testAuthenticateWithOpenIdWillFail()

public function testGetAc()
{
// We need to set the auth instance explicitly, since we need a auth storage
// other than the session storage.
$auth = Erfurt_Auth::getInstance();
$auth->setStorage(new Zend_Auth_Storage_NonPersistent());
Erfurt_App::getInstance()->setAuth($auth);

// We need to set the store
$storeAdapter = new Erfurt_Store_Adapter_Test();
$storeAdapter->getNewModel('http://localhost/OntoWiki/Config/');
$store = new Erfurt_Store(array('adapterInstance' => $storeAdapter), 'test');
Erfurt_App::getInstance()->setStore($store);

$ac = Erfurt_App::getInstance()->getAc();

if (!($ac instanceof Erfurt_Ac_Default)) {
$this->fail();
}
Expand Down

0 comments on commit ccc951c

Please sign in to comment.