]> source.dussan.org Git - nextcloud-server.git/commitdiff
Specify the email type and set name properly
authorThomas Müller <thomas.mueller@tmit.eu>
Mon, 30 Nov 2015 21:28:36 +0000 (22:28 +0100)
committerThomas Müller <thomas.mueller@tmit.eu>
Wed, 2 Dec 2015 08:14:41 +0000 (09:14 +0100)
apps/dav/lib/carddav/converter.php
apps/dav/tests/unit/carddav/convertertest.php

index 3aa3639ffa39195aa786155ea2b8547364be6dc8..eb2ee1f0604d90afec3311e419832a3bdeb2f2ae 100644 (file)
@@ -43,9 +43,10 @@ class Converter {
                $vCard->add(new Text($vCard, 'UID', $uid));
                if (!empty($displayName)) {
                        $vCard->add(new Text($vCard, 'FN', $displayName));
+                       $vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName)));
                }
                if (!empty($emailAddress)) {
-                       $vCard->add(new Text($vCard, 'EMAIL', $emailAddress));
+                       $vCard->add(new Text($vCard, 'EMAIL', $emailAddress, ['TYPE' => 'OTHER']));
                }
                if (!empty($cloudId)) {
                        $vCard->add(new Text($vCard, 'CLOUD', $cloudId));
@@ -67,18 +68,24 @@ class Converter {
                $image = $user->getAvatarImage(-1);
 
                $updated = false;
-               if(!is_null($vCard->FN) && $vCard->FN->getValue() !== $displayName) {
+               if((is_null($vCard->FN) && !empty($image)) || (!is_null($vCard->FN) && $vCard->FN->getValue() !== $displayName)) {
                        $vCard->FN = new Text($vCard, 'FN', $displayName);
+                       unset($vCard->N);
+                       $vCard->add(new Text($vCard, 'N', $this->splitFullName($displayName)));
                        $updated = true;
                }
-               if(!is_null($vCard->EMail) && $vCard->EMail->getValue() !== $emailAddress) {
+               if((is_null($vCard->EMail) && !empty($image)) || (!is_null($vCard->EMail) && $vCard->EMail->getValue() !== $emailAddress)) {
                        $vCard->EMAIL = new Text($vCard, 'EMAIL', $emailAddress);
                        $updated = true;
                }
-               if(!is_null($vCard->CLOUD) && $vCard->CLOUD->getValue() !== $cloudId) {
+               if((is_null($vCard->CLOUD) && !empty($image)) || (!is_null($vCard->CLOUD) && $vCard->CLOUD->getValue() !== $cloudId)) {
                        $vCard->CLOUD = new Text($vCard, 'CLOUD', $cloudId);
                        $updated = true;
                }
+               if((is_null($vCard->PHOTO) && !empty($image)) || (!is_null($vCard->PHOTO) && $vCard->PHOTO->getValue() !== $image)) {
+                       $vCard->add('PHOTO', $image->data(), ['ENCODING' => 'b', 'TYPE' => $image->mimeType()]);
+                       $updated = true;
+               }
 
                if (empty($emailAddress) && !is_null($vCard->EMAIL)) {
                        unset($vCard->EMAIL);
@@ -95,4 +102,28 @@ class Converter {
 
                return $updated;
        }
+
+       /**
+        * @param string $fullName
+        * @return string[]
+        */
+       public function splitFullName($fullName) {
+               // Very basic western style parsing. I'm not gonna implement
+               // https://github.com/android/platform_packages_providers_contactsprovider/blob/master/src/com/android/providers/contacts/NameSplitter.java ;)
+
+               $elements = explode(' ', $fullName);
+               $result = ['', '', '', '', ''];
+               if (count($elements) > 2) {
+                       $result[0] = implode(' ', array_slice($elements, count($elements)-1));
+                       $result[1] = $elements[0];
+                       $result[2] = implode(' ', array_slice($elements, 1, count($elements)-2));
+               } elseif (count($elements) === 2) {
+                       $result[0] = $elements[1];
+                       $result[1] = $elements[0];
+               } else {
+                       $result[0] = $elements[0];
+               }
+
+               return $result;
+       }
 }
index cb68326a7bc89e3178d082964f0de410a4cd2781..b4a0ee2a664d02e0c708560685a952b34fa36001 100644 (file)
@@ -45,9 +45,10 @@ class ConverterTests extends  TestCase {
 
        public function providesNewUsers() {
                return [
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nEND:VCARD\r\n"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nCLOUD:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEMAIL;TYPE=OTHER:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nCLOUD:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
                ];
        }
 
@@ -72,10 +73,10 @@ class ConverterTests extends  TestCase {
 
        public function providesUsersForUpdate() {
                return [
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nEND:VCARD\r\n"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nEMAIL:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nCLOUD:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nEMAIL;TYPE=OTHER:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:Dr. Foo Bar\r\nN:Bar;Dr.;Foo;;\r\nCLOUD:foo@bar.net\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
                ];
        }
 
@@ -107,10 +108,30 @@ class ConverterTests extends  TestCase {
 
        public function providesUsersForUpdateOfRemovedElement() {
                return [
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
-                       ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", "foo@bar.net"],
+                               ["BEGIN:VCARD\r\nVERSION:3.0\r\nPRODID:-//Sabre//Sabre VObject 3.4.7//EN\r\nUID:12345\r\nFN:12345\r\nN:12345;;;;\r\nEND:VCARD\r\n", "Dr. Foo Bar", null, "foo@bar.net"],
                ];
        }
 
+       /**
+        * @dataProvider providesNames
+        * @param $expected
+        * @param $fullName
+        */
+       public function testNameSplitter($expected, $fullName) {
+
+               $converter = new Converter();
+               $r = $converter->splitFullName($fullName);
+               $r = implode(';', $r);
+               $this->assertEquals($expected, $r);
+       }
+
+       public function providesNames() {
+               return [
+                               ['Sauron;;;;', 'Sauron'],
+                               ['Baggins;Bilbo;;;', 'Bilbo Baggins'],
+                               ['Tolkien;John;Ronald Reuel;;', 'John Ronald Reuel Tolkien'],
+               ];
+       }
 }