1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-28 10:24:11 +01:00

Fix to make it compatible with Sabre 2.1

Also fixed unit tests so they run but some still fail

- Added IUserSession mock
- Replaced OCP::getUser() with IUserSession usage to make the mock work
- Added base class for test cases
This commit is contained in:
Vincent Petry 2015-02-16 18:42:41 +01:00 committed by Vincent Petry
parent 4b2b12edb6
commit 0b5fb1bfc7
42 changed files with 210 additions and 235 deletions

View File

@ -12,23 +12,8 @@ namespace OCA\Contacts;
use \OC\AppFramework\Core\API;
//require_once __DIR__ . '/../lib/controller/pagecontroller.php';
\Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
\Sabre\VObject\Property::$classMap['CATEGORIES'] = '\OCA\Contacts\VObject\GroupProperty';
\Sabre\VObject\Property::$classMap['FN'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['TITLE'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['ROLE'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['NOTE'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['NICKNAME'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['EMAIL'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['TEL'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['IMPP'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['URL'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['LABEL'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['X-EVOLUTION-FILE-AS'] = '\OC\VObject\StringProperty';
\Sabre\VObject\Property::$classMap['N'] = '\OC\VObject\CompoundProperty';
\Sabre\VObject\Property::$classMap['ADR'] = '\OC\VObject\CompoundProperty';
\Sabre\VObject\Property::$classMap['GEO'] = '\OC\VObject\CompoundProperty';
\Sabre\VObject\Property::$classMap['ORG'] = '\OC\VObject\CompoundProperty';
\Sabre\VObject\Component\VCard::$componentMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
\Sabre\VObject\Component\VCard::$propertyMap['CATEGORIES'] = '\OCA\Contacts\VObject\GroupProperty';
\OC::$server->getNavigationManager()->add(array(
'id' => 'contacts',

View File

@ -4,7 +4,6 @@
<name>Contacts</name>
<licence>AGPL</licence>
<author>Jakob Sack,Thomas Tanghus</author>
<require>8</require>
<description>Address book with CardDAV support.</description>
<standalone/>
<documentation>
@ -14,5 +13,9 @@
<contacts>appinfo/remote.php</contacts>
<carddav>appinfo/remote.php</carddav>
</remote>
<dependencies>
<owncloud min-version="8" />
<php min-version="5.4" />
</dependencies>
<ocsid>166044</ocsid>
</info>

View File

@ -37,7 +37,7 @@ $RUNTIME_APPTYPES = array('authentication');
OC_App::loadApps($RUNTIME_APPTYPES);
// Backends
$authBackend = new OC_Connector_Sabre_Auth();
$authBackend = new \OC\Connector\Sabre\Auth();
$principalBackend = new \OC\Connector\Sabre\Principal(
\OC::$server->getConfig(),
\OC::$server->getUserManager()
@ -50,7 +50,6 @@ if (\OCP\Config::getAppValue('contacts', 'backend_ldap', "false") === "true") {
$backends[] = 'ldap';
}
$carddavBackend = new OCA\Contacts\CardDAV\Backend($backends);
$requestBackend = new OC_Connector_Sabre_Request();
// Root nodes
$principalCollection = new \Sabre\CalDAV\Principal\Collection($principalBackend);
@ -66,23 +65,19 @@ $nodes = array(
// Fire up server
$server = new \Sabre\DAV\Server($nodes);
$server->httpRequest = $requestBackend;
$server->httpRequest->setUrl(\OC::$server->getRequest()->getRequestUri());
$server->setBaseUri($baseuri);
// Add plugins
$server->addPlugin(new \Sabre\DAV\Auth\Plugin($authBackend, 'ownCloud'));
$server->addPlugin(new OCA\Contacts\CardDAV\Plugin());
$server->addPlugin(new \Sabre\DAVACL\Plugin());
$server->addPlugin(new \Sabre\DAV\Browser\Plugin(false)); // Show something in the Browser, but no upload
$server->addPlugin(new \Sabre\CardDAV\VCFExportPlugin());
$server->addPlugin(new OC_Connector_Sabre_ExceptionLoggerPlugin('carddav'));
$server->addPlugin(new \OC\Connector\Sabre\ExceptionLoggerPlugin('carddav'));
$server->addPlugin(new \OC\Connector\Sabre\AppEnabledPlugin(
'contacts',
OC::$server->getAppManager()
));
if (defined('DEBUG') && DEBUG) {
$server->debugExceptions = true;
}
// And off we go!
$server->exec();

View File

@ -447,17 +447,25 @@ $this->create('contacts_setpreference', 'preference/set')
}
);
$this->create('contacts_index_properties', 'indexproperties/{user}/')
->post()
->action(
function($params) {
\OC::$server->getSession()->close();
// TODO: Add BackgroundJob for this.
\OCP\Util::emitHook('OCA\Contacts', 'indexProperties', array());
$userSession = \OC::$server->getUserSession();
$currentUid = null;
if ($userSession->getUser() != null) {
$currentUid = $userSession->getUser()->getUid();
}
\OCP\Config::setUserValue($params['user'], 'contacts', 'contacts_properties_indexed', 'yes');
\OCP\JSON::success(array('isIndexed' => true));
}
)
->requirements(array('user'))
->defaults(array('user' => \OCP\User::getUser()));
if (!empty($currentUid)) {
$this->create('contacts_index_properties', 'indexproperties/{user}/')
->post()
->action(
function($params) {
\OC::$server->getSession()->close();
// TODO: Add BackgroundJob for this.
\OCP\Util::emitHook('OCA\Contacts', 'indexProperties', array());
\OCP\Config::setUserValue($params['user'], 'contacts', 'contacts_properties_indexed', 'yes');
\OCP\JSON::success(array('isIndexed' => true));
}
)
->requirements(array('user'))
->defaults(array('user' => $currentUid));
}

View File

@ -24,7 +24,7 @@ OCP\JSON::setContentTypeHeader('text/javascript');
OCP\JSON::checkLoggedIn();
OCP\JSON::checkAppEnabled('contacts');
$user = OCP\User::getUser();
$user = \OC::$server->getUserSession()->getUser()->getUId();
$groupsort = OCP\Config::getUserValue($user, 'contacts', 'groupsort', '');
$groupsort = explode(',', $groupsort);

View File

@ -129,7 +129,7 @@ class Addressbook extends AbstractPIMCollection {
public function getOwner() {
return isset($this->addressBookInfo['owner'])
? $this->addressBookInfo['owner']
: \OCP\User::getUser();
: \OC::$server->getUserSession()->getUser()->getUId();
}
/**

View File

@ -138,7 +138,7 @@ SQL;
$id = $row['contactid'];
$addressbookKey = $row['addressbookid'];
// Check if we are the owner of the contact
if ($row['userid'] !== \OCP\User::getUser()) {
if ($row['userid'] !== \OC::$server->getUserSession()->getUser()->getUId()) {
// we aren't the owner of the contact
try {
// it is possible that the contact is shared with us

View File

@ -65,7 +65,7 @@ class App {
$backendsTableName = '*PREFIX*addressbooks_backend',
$dbBackend = null
) {
$this->user = $user ? $user : \OCP\User::getUser();
$this->user = $user ? $user : \OC::$server->getUserSession()->getUser()->getUId();
$this->addressBooksTableName = $addressBooksTableName;
$this->backendsTableName = $backendsTableName;
$this->dbBackend = $dbBackend

View File

@ -80,7 +80,7 @@ abstract class AbstractBackend {
*
*/
public function __construct($userid = null) {
$this->userid = $userid ? $userid : \OCP\User::getUser();
$this->userid = $userid ? $userid : \OC::$server->getUserSession()->getUser()->getUId();
}
/**

View File

@ -245,7 +245,7 @@ class Ldap extends AbstractBackend {
$userid = null,
$addressBooksTableName = '*PREFIX*contacts_ldap_addressbooks'
) {
$this->userid = $userid ? $userid : \OCP\User::getUser();
$this->userid = $userid ? $userid : \OC::$server->getUserSession()->getUser()->getUId();
$this->addressbooks = array();
}

View File

@ -77,20 +77,22 @@ class AddressBook extends \Sabre\CardDAV\AddressBook {
$deleteprincipal = $this->getOwner();
$uid = $this->carddavBackend->userIDByPrincipal($this->getOwner());
$currentUid = \OC::$server->getUserSession()->getUser()->getUId();
$readWriteACL = array(
array(
'privilege' => '{DAV:}read',
'principal' => 'principals/' . \OCP\User::getUser(),
'principal' => 'principals/' . $currentUid,
'protected' => true,
),
array(
'privilege' => '{DAV:}write',
'principal' => 'principals/' . \OCP\User::getUser(),
'principal' => 'principals/' . $currentUid,
'protected' => true,
),
);
if($uid !== \OCP\User::getUser()) {
if($uid !== $currentUid) {
list(, $id) = explode('::', $this->addressBookInfo['id']);
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if($sharedAddressbook) {
@ -101,16 +103,16 @@ class AddressBook extends \Sabre\CardDAV\AddressBook {
return $readWriteACL;
}
if ($sharedAddressbook['permissions'] & \OCP\PERMISSION_CREATE) {
$createprincipal = 'principals/' . \OCP\User::getUser();
$createprincipal = 'principals/' . $currentUid;
}
if ($sharedAddressbook['permissions'] & \OCP\PERMISSION_READ) {
$readprincipal = 'principals/' . \OCP\User::getUser();
$readprincipal = 'principals/' . $currentUid;
}
if ($sharedAddressbook['permissions'] & \OCP\PERMISSION_UPDATE) {
$writeprincipal = 'principals/' . \OCP\User::getUser();
$writeprincipal = 'principals/' . $currentUid;
}
if ($sharedAddressbook['permissions'] & \OCP\PERMISSION_DELETE) {
$deleteprincipal = 'principals/' . \OCP\User::getUser();
$deleteprincipal = 'principals/' . $currentUid;
}
}
} else {

View File

@ -25,6 +25,7 @@
namespace OCA\Contacts\CardDAV;
use OCA\Contacts;
use \Sabre\DAV\PropPatch;
/**
* This class exchanges data between SabreDav and the Address book backends.
@ -55,7 +56,7 @@ class Backend extends \Sabre\CardDAV\Backend\AbstractBackend {
if (is_array($addressBooks)) {
foreach($addressBooks as $addressBook) {
if($addressBook['owner'] != \OCP\USER::getUser()) {
if($addressBook['owner'] != \OC::$server->getUserSession()->getUser()->getUId()) {
$addressBook['uri'] = $addressBook['uri'] . '_shared_by_' . $addressBook['owner'];
$addressBook['displayname'] = $addressBook['displayname'];
}
@ -85,13 +86,15 @@ class Backend extends \Sabre\CardDAV\Backend\AbstractBackend {
* well as the return value.
*
* @param mixed $addressbookid
* @param array $mutations
* @param PropPatch $mutations
* @see \Sabre\DAV\IProperties::updateProperties
* @return bool|array
*/
public function updateAddressBook($addressbookid, array $mutations) {
public function updateAddressBook($addressbookid, PropPatch $propPatch) {
$changes = array();
$mutations = $propPatch->getRemainingMutations();
foreach($mutations as $property=>$newvalue) {
switch($property) {
case '{DAV:}displayname' :

View File

@ -68,15 +68,16 @@ class Card extends \Sabre\CardDAV\Card {
$readprincipal = $this->getOwner();
$writeprincipal = $this->getOwner();
$uid = $this->carddavBackend->userIDByPrincipal($this->getOwner());
$currentUid = \OC::$server->getUserSession()->getUser()->getUId();
if($uid != \OCP\USER::getUser()) {
if($uid != $currentUid) {
list(, $id) = explode('::', $this->addressBookInfo['id']);
$sharedAddressbook = \OCP\Share::getItemSharedWithBySource('addressbook', $id);
if ($sharedAddressbook && ($sharedAddressbook['permissions'] & \OCP\PERMISSION_READ)) {
$readprincipal = 'principals/' . \OCP\USER::getUser();
$readprincipal = 'principals/' . $currentUid;
}
if ($sharedAddressbook && ($sharedAddressbook['permissions'] & \OCP\PERMISSION_UPDATE)) {
$writeprincipal = 'principals/' . \OCP\USER::getUser();
$writeprincipal = 'principals/' . $currentUid;
}
}

View File

@ -40,9 +40,10 @@ class Plugin extends \Sabre\CardDAV\Plugin {
* An exception is thrown if it's not.
*
* @param resource|string $data
* @param boolean $modified whether the data was modified
* @return void
*/
protected function validateVCard(&$data) {
protected function validateVCard(&$data, &$modified) {
// If it's a stream, we convert it to a string first.
if (is_resource($data)) {
@ -61,6 +62,7 @@ class Plugin extends \Sabre\CardDAV\Plugin {
throw new \Sabre\DAV\Exception\UnsupportedMediaType('This collection can only support vcard objects.');
}
$modified = true; // FIXME: set to false if neither repair nor upgrade was done
$vobj->validate(VCard::REPAIR|VCard::UPGRADE);
$data = $vobj->serialize();
}

View File

@ -90,7 +90,10 @@ abstract class ImportConnector {
} else {
$curVal = $property->getValue();
if ($curVal != '') {
$curVal .= ',' . StringUtil::convertToUTF8($value);
if (!is_array($curVal)) {
$curVal = array($curVal);
}
$curVal[] = StringUtil::convertToUTF8($value);
} else {
$curVal = StringUtil::convertToUTF8($value);
}
@ -139,7 +142,7 @@ abstract class ImportConnector {
}
foreach ($property->parameters as $parameter) {
// Filtering types
if ($parameter->name == 'TYPE' && !strcmp($parameter->value, $importEntry['type'])) {
if ($parameter->name == 'TYPE' && !strcmp($parameter->getValue(), $importEntry['type'])) {
$found=0;
if (isset($importEntry->additional_property)) {
// Filtering additional properties if necessary (I know, there are a lot of inner loops, sorry)
@ -162,7 +165,7 @@ abstract class ImportConnector {
}
// Property not found, creating one
$property = \Sabre\VObject\Property::create($importEntry['property']);
$property = $vcard->createProperty($importEntry['property']);
$vcard->add($property);
if ($importEntry['type']!=null) {
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($importEntry['type']));

View File

@ -125,7 +125,7 @@ class ImportCsvConnector extends ImportConnector {
* @return VCard, all unconverted elements are stored in X-Unknown-Element parameters
*/
public function convertElementToVCard($element, $title = null) {
$vcard = \Sabre\VObject\Component::create('VCARD');
$vcard = new \OCA\Contacts\VObject\VCard();
$nbElt = count($element);
for ($i=0; $i < $nbElt; $i++) {
@ -154,7 +154,7 @@ class ImportCsvConnector extends ImportConnector {
if (isset($importEntry->vcard_favourites)) {
foreach ($importEntry->vcard_favourites as $vcardFavourite) {
if (strcasecmp((string)$vcardFavourite, trim($oneValue)) == 0) {
$property = \Sabre\VObject\Property::create("X-FAVOURITES", 'yes');
$property = $vcard->createProperty("X-FAVOURITES", 'yes');
$vcard->add($property);
} else {
$property = $this->getOrCreateVCardProperty($vcard, $importEntry->vcard_entry);
@ -167,7 +167,7 @@ class ImportCsvConnector extends ImportConnector {
}
}
} else if (isset($element[$i]) && isset($title[$i])) {
$property = \Sabre\VObject\Property::create("X-Unknown-Element", StringUtil::convertToUTF8($element[$i]));
$property = $vcard->createProperty("X-Unknown-Element", StringUtil::convertToUTF8($element[$i]));
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($title[$i]));
$vcard->add($property);
}

View File

@ -117,7 +117,7 @@ class ImportLdifConnector extends ImportConnector{
* @return VCard
*/
public function convertElementToVCard($element) {
$dest = \Sabre\VObject\Component::create('VCARD');
$dest = new \OCA\Contacts\VObject\VCard();
foreach ($element as $ldifProperty) {
$importEntry = $this->getImportEntry($ldifProperty[0]);
@ -135,7 +135,7 @@ class ImportLdifConnector extends ImportConnector{
$this->convertElementToProperty($oneValue, $importEntry, $dest);
}
} else {
$property = \Sabre\VObject\Property::create("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1]));
$property = $dest->createProperty("X-Unknown-Element", ''.StringUtil::convertToUTF8($ldifProperty[1]));
$property->parameters[] = new \Sabre\VObject\Parameter('TYPE', ''.StringUtil::convertToUTF8($ldifProperty[0]));
$dest->add($property);
}
@ -156,7 +156,7 @@ class ImportLdifConnector extends ImportConnector{
if (isset($importEntry->vcard_favourites)) {
foreach ($importEntry->vcard_favourites as $vcardFavourite) {
if (strcasecmp((string)$vcardFavourite, trim($value)) == 0) {
$property = \Sabre\VObject\Property::create("X-FAVOURITES", 'yes');
$property = $dest->createProperty("X-FAVOURITES", 'yes');
$dest->add($property);
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);

View File

@ -111,14 +111,14 @@ class ImportVCardConnector extends ImportConnector{
} catch (VObject\ParseException $error) {
return false;
}
$dest = \Sabre\VObject\Component::create('VCARD');
$dest = new \OCA\Contacts\VObject\VCard();
foreach ($source->children() as $sourceProperty) {
$importEntry = $this->getImportEntry($sourceProperty, $source);
if ($importEntry) {
$value = $sourceProperty->value;
$value = $sourceProperty->getValue();
if (isset($importEntry['remove'])) {
$value = str_replace($importEntry['remove'], '', $sourceProperty->value);
$value = str_replace($importEntry['remove'], '', $sourceProperty->getValue());
}
$values = array($value);
if (isset($importEntry['separator'])) {
@ -129,7 +129,7 @@ class ImportVCardConnector extends ImportConnector{
if (isset($importEntry->vcard_favourites)) {
foreach ($importEntry->vcard_favourites as $vcardFavourite) {
if (strcasecmp((string)$vcardFavourite, trim($oneValue)) == 0) {
$property = \Sabre\VObject\Property::create("X-FAVOURITES", 'yes');
$property = $dest->createProperty("X-FAVOURITES", 'yes');
$dest->add($property);
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
@ -138,7 +138,7 @@ class ImportVCardConnector extends ImportConnector{
}
} else {
$property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
$this->updateProperty($property, $importEntry, $sourceProperty->value);
$this->updateProperty($property, $importEntry, $sourceProperty->getValue());
}
}
} else {
@ -166,7 +166,7 @@ class ImportVCardConnector extends ImportConnector{
$sourceGroupList = $vcard->select($groupEntry['property']);
if (count($sourceGroupList>0)) {
foreach ($sourceGroupList as $oneSourceGroup) {
if ($oneSourceGroup->value == $groupEntry['value'] && isset($oneSourceGroup->group) && isset($property->group) && $oneSourceGroup->group == $property->group) {
if ($oneSourceGroup->getValue() == $groupEntry['value'] && isset($oneSourceGroup->group) && isset($property->group) && $oneSourceGroup->group == $property->group) {
$numElt++;
}
}

View File

@ -49,7 +49,7 @@ class LdapConnector {
* @return OC_VCard
*/
public function ldapToVCard($ldapEntry) {
$vcard = \Sabre\VObject\Component::create('VCARD');
$vcard = new \OCA\Contacts\VObject\VCard();
$vcard->REV = $this->convertDate($ldapEntry['modifytimestamp'][0])->format(\DateTime::W3C);
//error_log("modifytimestamp: ".$vcard->REV);
$vcard->{'X-LDAP-DN'} = base64_encode($ldapEntry['dn']);
@ -116,9 +116,9 @@ class LdapConnector {
return $property;
}
foreach ($property->parameters as $parameter) {
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->value.' <> '.$v_param['type'], \OCP\Util::DEBUG);
if (!strcmp($parameter->value, $v_param['type'])) {
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->value.' found', \OCP\Util::DEBUG);
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' <> '.$v_param['type'], \OCP\Util::DEBUG);
if (!strcmp($parameter->getValue(), $v_param['type'])) {
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' parameter '.$parameter->getValue().' found', \OCP\Util::DEBUG);
if ($counter==$index) {
return $property;
}
@ -130,7 +130,7 @@ class LdapConnector {
// Property not found, creating one
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', create one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG);
$line = count($vcard->children) - 1;
$property = \Sabre\VObject\Property::create($v_param['property']);
$property = $vcard->createProperty($v_param['property']);
$vcard->add($property);
if ($v_param['type']!=null) {
//OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.', creating one '.$v_param['property'].';TYPE='.$v_param['type'], \OCP\Util::DEBUG);
@ -284,10 +284,10 @@ class LdapConnector {
}
} else {
// Last, if the ldif entry has a vcard_position set, take only the value in the position index
$value = $property->value;
$value = $property->getValue();
if (isset($ldifEntry['vcard_position'])) {
//\OC_Log::write('ldapconnector', __METHOD__." position set ".$ldifEntry['vcard_position'], \OC_Log::DEBUG);
$tmpValues = explode(";", $property->value);
$tmpValues = explode(";", $property->getValue());
$value = $tmpValues[$ldifEntry['vcard_position']];
}
//\OC_Log::write('ldapconnector', __METHOD__.__METHOD__." entry : ".$ldifEntry['name']." - value : $value", \OC_Log::DEBUG);

View File

@ -57,6 +57,8 @@ class Contact extends VObject\VCard implements IPIMObject {
* @param mixed $data
*/
public function __construct($parent, $backend, $data = null) {
parent::__construct('VCARD');
self::$l10n = $parent::$l10n;
//\OCP\Util::writeLog('contacts', __METHOD__ . ' , data: ' . print_r($data, true), \OCP\Util::DEBUG);
$this->props['parent'] = $parent;
@ -313,7 +315,7 @@ class Contact extends VObject\VCard implements IPIMObject {
* @return bool
*/
public function retrieve() {
if ($this->isRetrieved() || count($this->children) > 1) {
if ($this->isRetrieved()) {
//\OCP\Util::writeLog('contacts', __METHOD__. ' children', \OCP\Util::DEBUG);
return true;
} else {
@ -324,7 +326,7 @@ class Contact extends VObject\VCard implements IPIMObject {
$this->add($child);
if($child->name === 'FN') {
$this->props['displayname']
= strtr($child->value, array('\,' => ',', '\;' => ';', '\\\\' => '\\'));
= strtr($child->getValue(), array('\,' => ',', '\;' => ';', '\\\\' => '\\'));
}
}
$this->setRetrieved(true);
@ -374,7 +376,11 @@ class Contact extends VObject\VCard implements IPIMObject {
);
if ($obj) {
foreach ($obj->children as $child) {
$this->add($child);
if($child->name === 'VERSION' || $child->name === 'PRODID') {
parent::__set($child->name, $child);
} else {
$this->add($child);
}
}
$this->setRetrieved(true);
$this->setSaved(true);
@ -508,7 +514,7 @@ class Contact extends VObject\VCard implements IPIMObject {
*/
public function setPropertyByChecksum($checksum, $name, $value, $parameters=array()) {
if ($checksum === 'new') {
$property = Property::create($name);
$property = $this->createProperty($name);
$this->add($property);
} else {
$property = $this->getPropertyByChecksum($checksum);
@ -582,7 +588,7 @@ class Contact extends VObject\VCard implements IPIMObject {
case 'ORG':
$property = $this->select($name);
if (count($property) === 0) {
$property = \Sabre\VObject\Property::create($name);
$property = $this->createProperty($name);
$this->add($property);
} else {
// Actually no idea why this works
@ -715,7 +721,7 @@ class Contact extends VObject\VCard implements IPIMObject {
continue;
} else {
foreach ($ownproperties as $ownproperty) {
if (strtolower($property->value) === strtolower($ownproperty->value)) {
if (strtolower($property->getValue()) === strtolower($ownproperty->getValue())) {
// We already have this property, so skip both loops
continue 2;
}
@ -728,7 +734,7 @@ class Contact extends VObject\VCard implements IPIMObject {
$this->add($property);
$updated = true;
} else {
$this->setPropertyByName($property->name, $property->value, $property->parameters);
$this->setPropertyByName($property->name, $property->getValue(), $property->parameters);
}
}
}
@ -819,18 +825,18 @@ class Contact extends VObject\VCard implements IPIMObject {
} catch(\Exception $e) {
return;
}
$vevent = \Sabre\VObject\Component::create('VEVENT');
$vcal = new \Sabre\VObject\Component\VCalendar();
$vcal->VERSION = '2.0';
$vcal->createComponent('VEVENT');
$vevent->add('DTSTART');
$vevent->DTSTART->setDateTime(
$date,
\Sabre\VObject\Property\DateTime::DATE
$date
);
$event->DTSTART['VALUE'] = 'date';
$vevent->add('DURATION', 'P1D');
$vevent->{'UID'} = $this->UID;
$vevent->{'RRULE'} = 'FREQ=YEARLY';
$vevent->{'SUMMARY'} = $title . ' (' . $date->format('Y') . ')';
$vcal = \Sabre\VObject\Component::create('VCALENDAR');
$vcal->VERSION = '2.0';
$appinfo = \OCP\App::getAppInfo('contacts');
$appversion = \OCP\App::getAppVersion('contacts');
$vcal->PRODID = '-//ownCloud//NONSGML '.$appinfo['name'].' '.$appversion.'//EN';

View File

@ -53,12 +53,13 @@ class GroupController extends Controller {
$shares[$key]['length'] = count($children);
}
$currentUid = \OC::$server->getUserSession()->getUser()->getUId();
$groups = array(
'categories' => $tags,
'favorites' => $favorites,
'shared' => $shares,
'lastgroup' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'lastgroup', 'all'),
'sortorder' => \OCP\Config::getUserValue(\OCP\User::getUser(), 'contacts', 'groupsort', ''),
'lastgroup' => \OCP\Config::getUserValue($currentUid, 'contacts', 'lastgroup', 'all'),
'sortorder' => \OCP\Config::getUserValue($currentUid, 'contacts', 'groupsort', ''),
);
return new JSONResponse($groups);
@ -348,7 +349,7 @@ class GroupController extends Controller {
* in parentheses, like "Tag (owner)".
*/
private function displayName($tag) {
if ($tag['owner'] != \OCP\User::getUser()) {
if ($tag['owner'] != \OC::$server->getUserSession()->getUser()->getUId()) {
return $tag['name'] . ' ('. $tag['owner'] . ')';
}
return $tag['name'];

View File

@ -159,7 +159,7 @@ class ImportController extends Controller {
$request = $this->request;
$response = new JSONResponse();
$params = $this->request->urlParams;
$app = new App(\OCP\User::getUser());
$app = new App(\OC::$server->getUserSession()->getUser()->getUId());
$addressBookId = $params['addressBookId'];
$format = $params['importType'];

View File

@ -40,7 +40,7 @@ class SettingsController extends Controller {
$response->bailOut(App::$l10n->t('No value is given.'));
}
if(\OCP\Config::setUserValue(\OCP\User::getUser(), 'contacts', $key, $value)) {
if(\OCP\Config::setUserValue(\OC::$server->getUserSession()->getUser()->getUId(), 'contacts', $key, $value)) {
$response->setParams(array(
'key' => $key,
'value' => $value)
@ -52,4 +52,4 @@ class SettingsController extends Controller {
);
}
}
}
}

View File

@ -22,7 +22,7 @@ class Addressbook implements \OCP\Share_Backend_Collection {
public $app;
public function __construct() {
$this->app = new App(\OCP\User::getUser());
$this->app = new App(\OC::$server->getUserSession()->getUser()->getUId());
}
/**

View File

@ -37,7 +37,7 @@ class Contact implements \OCP\Share_Backend {
public $backend;
public function __construct() {
$this->app = new App(\OCP\User::getUser());
$this->app = new App(\OC::$server->getUserSession()->getUser()->getUId());
$this->backend = $this->app->getBackend('local');
}

View File

@ -106,7 +106,7 @@ class JSONSerializer {
if(!is_null($temp)) {
// Get Apple X-ABLabels
if(isset($contact->{$property->group . '.X-ABLABEL'})) {
$temp['label'] = $contact->{$property->group . '.X-ABLABEL'}->value;
$temp['label'] = $contact->{$property->group . '.X-ABLABEL'}->getValue();
if($temp['label'] == '_$!<Other>!$_') {
$temp['label'] = Properties::$l10n->t('Other');
}
@ -142,7 +142,7 @@ class JSONSerializer {
if(!in_array($property->name, Properties::$indexProperties)) {
return;
}
$value = $property->value;
$value = $property->getValue();
if($property->name == 'ADR' || $property->name == 'N' || $property->name == 'ORG' || $property->name == 'CATEGORIES') {
$value = $property->getParts();
$value = array_map('trim', $value);
@ -195,15 +195,15 @@ class JSONSerializer {
// Faulty entries by kaddressbook
// Actually TYPE=PREF is correct according to RFC 2426
// but this way is more handy in the UI. Tanghus.
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
if($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') {
$parameter->name = 'PREF';
$parameter->value = '1';
$parameter->setValue('1');
}
// NOTE: Apparently \Sabre\VObject\Reader can't always deal with value list parameters
// like TYPE=HOME,CELL,VOICE. Tanghus.
// TODO: Check if parameter is has commas and split + merge if so.
if ($parameter->name == 'TYPE') {
$pvalue = $parameter->value;
$pvalue = $parameter->getValue();
if(is_string($pvalue) && strpos($pvalue, ',') !== false) {
$pvalue = array_map('trim', explode(',', $pvalue));
}
@ -216,7 +216,7 @@ class JSONSerializer {
}
}
else{
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($parameter->value);
$temp['parameters'][$parameter->name] = \OCP\Util::sanitizeHTML($parameter->getValue());
}
}
return $temp;

View File

@ -260,7 +260,7 @@ Class Properties {
}
$preferred = 0;
foreach($property->parameters as $parameter) {
if($parameter->name == 'TYPE' && strtoupper($parameter->value) == 'PREF') {
if($parameter->name == 'TYPE' && strtoupper($parameter->getValue()) == 'PREF') {
$preferred = 1;
break;
}
@ -268,10 +268,10 @@ Class Properties {
try {
$result = self::$updateindexstmt->execute(
array(
\OCP\User::getUser(),
\OC::$server->getUserSession()->getUser()->getUId(),
$contactid,
$property->name,
substr($property->value, 0, 254),
substr($property->getValue(), 0, 254),
$preferred,
)
);

View File

@ -29,7 +29,7 @@ use OC\VObject\CompoundProperty;
*
* NOTE: Group names are case-insensitive.
*/
class GroupProperty extends CompoundProperty {
class GroupProperty extends \Sabre\VObject\Property\Text {
/**
* Add a group.
@ -117,4 +117,4 @@ class GroupProperty extends CompoundProperty {
}
return array_search(strtolower($needle), array_map('strtolower', $haystack));
}
}
}

View File

@ -1,82 +0,0 @@
<?php
/**
* ownCloud - VObject String Property
*
* This class adds escaping/unescaping of simple string properties.
*
* @author Thomas Tanghus
* @author Evert Pot (http://www.rooftopsolutions.nl/)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OCA\Contacts\VObject;
use Sabre\VObject;
/**
* This class overrides \Sabre\VObject\Property::serialize() to properly
* escape commas and semi-colons in string properties.
*/
class StringProperty extends VObject\Property {
/**
* Turns the object back into a serialized clob.
*
* @return string
*/
public function serialize() {
$str = $this->name;
if ($this->group) {
$str = $this->group . '.' . $this->name;
}
foreach($this->parameters as $param) {
$str.=';' . $param->serialize();
}
$src = array(
'\\',
"\n",
';',
',',
);
$out = array(
'\\\\',
'\n',
'\;',
'\,',
);
$value = strtr($this->value, array('\,' => ',', '\;' => ';', '\\\\' => '\\'));
$str.=':' . str_replace($src, $out, $value);
$out = '';
while(strlen($str) > 0) {
if (strlen($str) > 75) {
$out .= mb_strcut($str, 0, 75, 'utf-8') . "\r\n";
$str = ' ' . mb_strcut($str, 75, strlen($str), 'utf-8');
} else {
$out .= $str . "\r\n";
$str = '';
break;
}
}
return $out;
}
}

View File

@ -121,22 +121,22 @@ class VCard extends VObject\Component\VCard {
foreach($property->parameters as $key=>&$parameter) {
// Check for values without names which Sabre interprets
// as names without values.
if(trim($parameter->value) === '') {
$parameter->value = $parameter->name;
if(trim($parameter->getValue()) === '') {
$parameter->setValue($parameter->name);
$parameter->name = $this->paramName($parameter->name);
}
// Check out for encoded string and decode them :-[
if(strtoupper($parameter->name) == 'ENCODING') {
if(strtoupper($parameter->value) == 'QUOTED-PRINTABLE') {
$property->value = str_replace(
if(strtoupper($parameter->getValue()) == 'QUOTED-PRINTABLE') {
$property->setValue(str_replace(
"\r\n", "\n",
VObject\StringUtil::convertToUTF8(
quoted_printable_decode($property->value)
quoted_printable_decode($property->getValue())
)
);
));
unset($property->parameters[$key]);
} elseif(strtoupper($parameter->value) == 'BASE64') {
$parameter->value = 'b';
} elseif(strtoupper($parameter->getValue()) == 'BASE64') {
$parameter->setValue('b');
}
} elseif(strtoupper($parameter->name) == 'CHARSET') {
unset($property->parameters[$key]);
@ -154,12 +154,12 @@ class VCard extends VObject\Component\VCard {
// Work around issue in older VObject sersions
// https://github.com/fruux/sabre-vobject/issues/24
foreach($property->parameters as $key=>$parameter) {
if(strpos($parameter->value, ',') === false) {
if(strpos($parameter->getValue(), ',') === false) {
continue;
}
$values = explode(',', $parameter->value);
$values = explode(',', $parameter->getValue());
$values = array_map('trim', $values);
$parameter->value = array_shift($values);
$parameter->setValue(array_shift($values));
foreach($values as $value) {
$property->add($parameter->name, $value);
}
@ -305,7 +305,7 @@ class VCard extends VObject\Component\VCard {
if(count($slice) < 2) { // If not enought, add one more...
$slice[] = "";
}
$this->N = implode(';', $slice).';;;';
$this->N = $slice;
}
}

View File

@ -1,6 +1,6 @@
<?php
$tmpl = new OCP\Template( 'contacts', 'settings');
$tmpl->assign('addressbooks', OCA\Contacts\Addressbook::all(OCP\USER::getUser()));
$tmpl->assign('addressbooks', OCA\Contacts\Addressbook::all(\OC::$server->getUserSession()->getUser()->getUId()));
$tmpl->printPage();

View File

@ -449,7 +449,7 @@ vendor_style('contacts', array(
</li>
</div>
<div class="adr" type="text/template">
<li data-element="adr" data-checksum="{checksum}" data-lang="<?php p(OCP\Config::getUserValue(OCP\USER::getUser(), 'core', 'lang', 'en')); ?>" class="propertycontainer">
<li data-element="adr" data-checksum="{checksum}" data-lang="<?php p(OCP\Config::getUserValue(\OC::$server->getUserSession()->getUser()->getUId(), 'core', 'lang', 'en')); ?>" class="propertycontainer">
<span class="parameters">
<input type="checkbox" id="adr_pref_{idx}" class="parameter tooltipped downwards" data-parameter="TYPE" name="parameters[TYPE][]" value="PREF" title="<?php p($l->t('Preferred')); ?>" />
<select class="type parameter" data-parameter="TYPE" name="parameters[TYPE][]">
@ -491,7 +491,7 @@ vendor_style('contacts', array(
</li>
</div>
<!--
<li data-element="adr" data-checksum="{checksum}" data-lang="<?php p(OCP\Config::getUserValue(OCP\USER::getUser(), 'core', 'lang', 'en')); ?>" class="propertycontainer">
<li data-element="adr" data-checksum="{checksum}" data-lang="<?php p(OCP\Config::getUserValue(\OC::$server->getUserSession()->getUser()->getUId(), 'core', 'lang', 'en')); ?>" class="propertycontainer">
<span class="parameters">
<input type="checkbox" id="adr_pref_{idx}" class="parameter tooltipped downwards" data-parameter="TYPE" name="parameters[TYPE][]" value="PREF" title="<?php p($l->t('Preferred')); ?>" />
<select class="type parameter" data-parameter="TYPE" name="parameters[TYPE][]">

View File

@ -12,6 +12,8 @@ if(!class_exists('PHPUnit_Framework_TestCase')) {
require_once('PHPUnit/Autoload.php');
}
include_once('lib/testcase.php');
//OC_App::enable('contacts');
OC_Hook::clear();

View File

@ -12,7 +12,7 @@ use Sabre\VObject\Reader;
require_once __DIR__ . '/backend/mock.php';
class AddressBookTest extends \PHPUnit_Framework_TestCase {
class AddressBookTest extends TestCase {
/**
* @var array
@ -28,8 +28,7 @@ class AddressBookTest extends \PHPUnit_Framework_TestCase {
protected $backend;
public function setUp() {
\Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
parent::setUp();
$this->backend = new Backend\Mock('foobar');
$this->abinfo = $this->backend->getAddressBook('foo');
@ -40,6 +39,8 @@ class AddressBookTest extends \PHPUnit_Framework_TestCase {
public function tearDown() {
unset($this->backend);
unset($this->ab);
parent::tearDown();
}
public function testGetDisplayName() {

View File

@ -9,7 +9,7 @@
namespace OCA\Contacts;
class AddressBookProviderTest extends \PHPUnit_Framework_TestCase {
class AddressBookProviderTest extends TestCase {
/**
* @var array
@ -35,17 +35,15 @@ class AddressBookProviderTest extends \PHPUnit_Framework_TestCase {
private $contactIds = array();
public function setUp() {
parent::setUp();
\Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
$user = uniqid('user_');
$this->backend = new Backend\Database($user);
$this->backend = new Backend\Database($this->testUser);
$this->abinfo = array('displayname' => uniqid('display_'));
$this->ab = new AddressBook($this->backend, $this->abinfo);
$this->provider = new AddressbookProvider($this->ab);
$card = \Sabre\VObject\Component::create('VCARD');
$card = new \OCA\Contacts\VObject\VCard();
$uid = substr(md5(rand().time()), 0, 10);
$card->add('UID', $uid);
$card->add('FN', 'Max Mustermann');
@ -54,21 +52,21 @@ class AddressBookProviderTest extends \PHPUnit_Framework_TestCase {
$this->contactIds[] = $id;
// Add extra contact
$card = \Sabre\VObject\Component::create('VCARD');
$card = new \OCA\Contacts\VObject\VCard();
$uid = substr(md5(rand().time()), 0, 10);
$card->add('UID', $uid);
$card->add('FN', 'Jan Janssens');
$id = $this->ab->addChild($card);
Utils\Properties::updateIndex($id, $card);
$this->ab->deleteChild($id);
}
public function tearDown() {
unset($this->backend);
unset($this->ab);
Utils\Properties::purgeIndexes($this->contactIds);
parent::tearDown();
}
public function testSearch() {

View File

@ -12,7 +12,7 @@ use Sabre\VObject\Reader;
require_once __DIR__ .'/mock.php';
class BackendTest extends \PHPUnit_Framework_TestCase {
class BackendTest extends \OCA\Contacts\TestCase {
/**
* @var array

View File

@ -17,7 +17,7 @@ class Mock extends AbstractBackend {
function __construct($userid = null, $addressBooks = null, $contacts = null) {
$this->userid = $userid ? $userid : \OCP\User::getUser();
$this->userid = $userid ? $userid : \OC::$server->getUserSession()->getUser()->getUId();
$this->addressBooks = $addressBooks;
$this->contacts = $contacts;

View File

@ -13,7 +13,7 @@ use OCA\Contacts\Utils\JSONSerializer;
require_once __DIR__ . '/backend/mock.php';
class ContactTest extends \PHPUnit_Framework_TestCase {
class ContactTest extends TestCase {
/**
* @var array
@ -33,7 +33,7 @@ class ContactTest extends \PHPUnit_Framework_TestCase {
protected $backend;
public function setUp() {
parent::setUp();
$this->backend = new Backend\Mock('foobar');
$this->abinfo = $this->backend->getAddressBook('foo');
$this->ab = new AddressBook($this->backend, $this->abinfo);
@ -44,6 +44,7 @@ class ContactTest extends \PHPUnit_Framework_TestCase {
unset($this->backend);
unset($this->ab);
unset($this->contact);
parent::tearDown();
}
public function testGetDisplayName() {

View File

@ -6,7 +6,7 @@
* See the COPYING-README file.
*/
OC_App::loadApp('contacts');
class Test_Contacts_Backend_Datebase extends PHPUnit_Framework_TestCase {
class Test_Contacts_Backend_Datebase extends \OCA\Contacts\TestCase {
protected static $schema_file = 'static://test_db_scheme';
protected static $test_prefix;
protected static $backend;

46
tests/lib/testcase.php Normal file
View File

@ -0,0 +1,46 @@
<?php
/**
* Copyright (c) 2015 Vincent Petry <pvince81@owncloud.com>
* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace OCA\Contacts;
class TestCase extends \PHPUnit_Framework_TestCase {
/**
* @var string
*/
protected $testUser;
public function setUp() {
$this->testUser = uniqid('user_');
// needed because some parts of code call "getRequest()" and "getSession()"
$session = $this->getMock('\OC\Session\Memory', array(), array(''));
$session->expects($this->any())
->method('get')
->with('user_id')
->will($this->returnValue($this->testUser));
$userObject = $this->getMock('\OCP\IUser');
$userObject->expects($this->any())
->method('getUId')
->will($this->returnValue($this->testUser));
$userSession = $this->getMockBuilder('\OC\User\Session')
->disableOriginalConstructor()
->getMock();
$userSession->expects($this->any())
->method('getUser')
->will($this->returnValue($userObject));
$userSession->expects($this->any())
->method('getSession')
->will($this->returnValue($session));
\OC::$server->registerService('UserSession', function (\OCP\IServerContainer $c) use ($userSession){
return $userSession;
});
}
}

View File

@ -6,12 +6,10 @@
* See the COPYING-README file.
*/
class Test_VObjects extends PHPUnit_Framework_TestCase {
class Test_VObjects extends \OCA\Contacts\TestCase {
public static function setUpBeforeClass() {
\Sabre\VObject\Component::$classMap['VCARD'] = '\OCA\Contacts\VObject\VCard';
\Sabre\VObject\Property::$classMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty';
\Sabre\VObject\Component\VCard::$propertyMap['CATEGORIES'] = 'OCA\Contacts\VObject\GroupProperty';
}
public function testCrappyVCard() {
@ -47,11 +45,13 @@ class Test_VObjects extends PHPUnit_Framework_TestCase {
'Friends, Family',
);
$property = \Sabre\VObject\Property::create('CATEGORIES');
$vcard = new \OCA\Contacts\VObject\VCard();
$property = $vcard->createProperty('CATEGORIES');
$property->setParts($arr);
// Test parsing and serializing
$this->assertEquals('Home,work,Friends\, Family', $property->value);
$this->assertEquals('Home,work,Friends\, Family', $property->getValue());
$this->assertEquals('CATEGORIES:Home,work,Friends\, Family' . "\r\n", $property->serialize());
$this->assertEquals(3, count($property->getParts()));
@ -59,7 +59,7 @@ class Test_VObjects extends PHPUnit_Framework_TestCase {
$property->addGroup('Coworkers');
$this->assertTrue($property->hasGroup('coworkers'));
$this->assertEquals(4, count($property->getParts()));
$this->assertEquals('Home,work,Friends\, Family,Coworkers', $property->value);
$this->assertEquals('Home,work,Friends\, Family,Coworkers', $property->getValue());
// Test remove
$this->assertTrue($property->hasGroup('Friends, fAmIlY'));
@ -74,4 +74,4 @@ class Test_VObjects extends PHPUnit_Framework_TestCase {
$this->assertEquals('Work', $parts[1]);
//$this->assertEquals(true, false);
}
}
}

View File

@ -1,6 +1,6 @@
<?php
$CONFIG = array (
'loglevel' => '0',
'loglevel' => '1',
'appstoreenabled' => false,
'apps_paths' =>
array (