public function setUp()
 {
     parent::setUp();
     $this->testUser = $this->getUniqueID('user_');
     // needed because some parts of code call "getRequest()" and "getSession()"
     $session = $this->getMockBuilder('\\OC\\Session\\Memory')->disableOriginalConstructor()->getMock();
     $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;
     });
     $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 = new \OCA\Contacts\VObject\VCard();
     $uid = substr(md5($this->getUniqueID()), 0, 10);
     $card->add('UID', $uid);
     $card->add('FN', 'Max Mustermann');
     $id = $this->ab->addChild($card);
     Utils\Properties::updateIndex($id, $card);
     $this->contactIds[] = $id;
     // Add extra contact
     $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->contactIds[] = $id;
 }
Esempio n. 2
0
 public function testGroupProperty()
 {
     $arr = array('Home', 'work', 'Friends, Family');
     $vcard = new \OCA\Contacts\VObject\VCard();
     $property = $vcard->createProperty('CATEGORIES');
     $property->setParts($arr);
     // Test parsing and serializing
     $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()));
     // Test add
     $property->addGroup('Coworkers');
     $this->assertTrue($property->hasGroup('coworkers'));
     $this->assertEquals(4, count($property->getParts()));
     $this->assertEquals('Home,work,Friends\\, Family,Coworkers', $property->getValue());
     // Test remove
     $this->assertTrue($property->hasGroup('Friends, fAmIlY'));
     $property->removeGroup('Friends, fAmIlY');
     $this->assertEquals(3, count($property->getParts()));
     $parts = $property->getParts();
     $this->assertEquals('Coworkers', $parts[2]);
     // Test rename
     $property->renameGroup('work', 'Work');
     $parts = $property->getParts();
     $this->assertEquals('Work', $parts[1]);
     //$this->assertEquals(true, false);
 }
 /**
  * @brief transform a ldap entry into an VCard object
  *	for each ldap entry which is like "property: value"
  *	to a VCard entry which is like "PROPERTY[;PARAMETER=param]:value"
  * @param array $ldapEntry
  * @return OC_VCard
  */
 public function ldapToVCard($ldapEntry)
 {
     $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']);
     // OCP\Util::writeLog('ldap_vcard_connector', __METHOD__.' vcard is '.$vcard->serialize(), \OCP\Util::DEBUG);
     for ($i = 0; $i < $ldapEntry["count"]; $i++) {
         // ldap property name : $ldap_entry[$i]
         $lProperty = $ldapEntry[$i];
         for ($j = 0; $j < $ldapEntry[$lProperty]["count"]; $j++) {
             // What to do :
             // convert the ldap property into vcard property, type and position (if needed)
             // $v_params format: array('property' => property, 'type' => array(types), 'position' => position)
             $v_params = $this->getVCardProperty($lProperty);
             foreach ($v_params as $v_param) {
                 if (isset($v_param['unassigned'])) {
                     // if the value comes from the unassigned entry, it's a vcard property dumped
                     try {
                         $property = \Sabre\VObject\Reader::read($ldapEntry[$lProperty][$j]);
                         $vcard->add($property);
                     } catch (exception $e) {
                     }
                 } else {
                     // Checks if a same kind of property already exists in the VCard (property and parameters)
                     // if so, sets a property variable with the current data
                     // else, creates a property variable
                     $v_property = $this->getOrCreateVCardProperty($vcard, $v_param, $j);
                     // modify the property with the new data
                     if (strcasecmp($v_param['image'], 'true') == 0) {
                         $this->updateVCardImageProperty($v_property, $ldapEntry[$lProperty][$j], $vcard->VERSION);
                     } else {
                         $this->updateVCardProperty($v_property, $ldapEntry[$lProperty][$j], $v_param['position']);
                     }
                 }
             }
         }
     }
     if (!isset($vcard->UID)) {
         $vcard->UID = base64_encode($ldapEntry['dn']);
     }
     return $vcard;
 }
 public function setUp()
 {
     parent::setUp();
     $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 = new \OCA\Contacts\VObject\VCard();
     $uid = substr(md5(rand() . time()), 0, 10);
     $card->add('UID', $uid);
     $card->add('FN', 'Max Mustermann');
     $id = $this->ab->addChild($card);
     Utils\Properties::updateIndex($id, $card);
     $this->contactIds[] = $id;
     // Add extra contact
     $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);
 }
 /**
  * @brief converts a VCard into a owncloud VCard
  * @param $element the VCard element to convert
  * @return VCard|false
  */
 public function convertElementToVCard($element)
 {
     try {
         $source = VObject\Reader::read($element, VObject\Reader::OPTION_FORGIVING);
     } catch (VObject\ParseException $error) {
         return false;
     }
     $dest = new \OCA\Contacts\VObject\VCard();
     foreach ($source->children() as $sourceProperty) {
         $importEntry = $this->getImportEntry($sourceProperty, $source);
         if ($importEntry) {
             $value = $sourceProperty->getValue();
             if (isset($importEntry['remove'])) {
                 $value = str_replace($importEntry['remove'], '', $sourceProperty->getValue());
             }
             $values = array($value);
             if (isset($importEntry['separator'])) {
                 $values = explode($importEntry['separator'], $value);
             }
             foreach ($values as $oneValue) {
                 if (isset($importEntry->vcard_favourites)) {
                     foreach ($importEntry->vcard_favourites as $vcardFavourite) {
                         if (strcasecmp((string) $vcardFavourite, trim($oneValue)) == 0) {
                             $property = $dest->createProperty("X-FAVOURITES", 'yes');
                             $dest->add($property);
                         } else {
                             $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
                             $this->updateProperty($property, $importEntry, trim($oneValue));
                         }
                     }
                 } else {
                     $property = $this->getOrCreateVCardProperty($dest, $importEntry->vcard_entry);
                     $this->updateProperty($property, $importEntry, $sourceProperty->getValue());
                 }
             }
         } else {
             $property = clone $sourceProperty;
             $dest->add($property);
         }
     }
     $dest->validate(\Sabre\VObject\Component\VCard::REPAIR);
     return $dest;
 }
 /**
  * @brief converts a unique element into a owncloud VCard
  * @param $element the element to convert
  * @return VCard, all unconverted elements are stored in X-Unknown-Element parameters
  */
 public function convertElementToVCard($element, $title = null)
 {
     $vcard = new \OCA\Contacts\VObject\VCard();
     $nbElt = count($element);
     for ($i = 0; $i < $nbElt; $i++) {
         if ($element[$i] != '') {
             //$importEntry = false;
             // Look for the right import_entry
             if (isset($this->configContent->import_core->base_parsing)) {
                 if (strcasecmp((string) $this->configContent->import_core->base_parsing, 'position') == 0) {
                     $importEntry = $this->getImportEntryFromPosition((string) $i);
                 } else {
                     if (strcasecmp((string) $this->configContent->import_core->base_parsing, 'name') == 0 && isset($title[$i])) {
                         $importEntry = $this->getImportEntryFromName($title[$i]);
                     }
                 }
             }
             if ($importEntry) {
                 // Create a new property and attach it to the vcard
                 $value = $element[$i];
                 if (isset($importEntry['remove'])) {
                     $value = str_replace($importEntry['remove'], '', $element[$i]);
                 }
                 $values = array($value);
                 if (isset($importEntry['separator'])) {
                     $values = explode($importEntry['separator'], $value);
                 }
                 foreach ($values as $oneValue) {
                     if (isset($importEntry->vcard_favourites)) {
                         foreach ($importEntry->vcard_favourites as $vcardFavourite) {
                             if (strcasecmp((string) $vcardFavourite, trim($oneValue)) == 0) {
                                 $property = $vcard->createProperty("X-FAVOURITES", 'yes');
                                 $vcard->add($property);
                             } else {
                                 $property = $this->getOrCreateVCardProperty($vcard, $importEntry->vcard_entry);
                                 $this->updateProperty($property, $importEntry, trim($oneValue));
                             }
                         }
                     } else {
                         $property = $this->getOrCreateVCardProperty($vcard, $importEntry->vcard_entry);
                         $this->updateProperty($property, $importEntry, trim($oneValue));
                     }
                 }
             } else {
                 if (isset($element[$i]) && isset($title[$i])) {
                     $property = $vcard->createProperty("X-Unknown-Element", StringUtil::convertToUTF8($element[$i]));
                     $property->add('TYPE', StringUtil::convertToUTF8($title[$i]));
                     $vcard->add($property);
                 }
             }
         }
     }
     $vcard->validate(\Sabre\VObject\Component\VCard::REPAIR);
     return $vcard;
 }