2017-09-12 17:40:46 -04:00
< ? php
2025-06-30 09:04:05 -04:00
2017-09-12 17:40:46 -04:00
/**
2024-05-10 09:09:14 -04:00
* SPDX - FileCopyrightText : 2017 Nextcloud GmbH and Nextcloud contributors
* SPDX - License - Identifier : AGPL - 3.0 - or - later
2017-09-12 17:40:46 -04:00
*/
namespace Test\Collaboration\Collaborators ;
use OC\Collaboration\Collaborators\MailPlugin ;
use OC\Collaboration\Collaborators\SearchResult ;
use OC\Federation\CloudIdManager ;
2021-03-09 15:48:48 -05:00
use OC\KnownUser\KnownUserService ;
2017-09-12 17:40:46 -04:00
use OCP\Collaboration\Collaborators\SearchResultType ;
use OCP\Contacts\IManager ;
2022-08-30 12:15:02 -04:00
use OCP\EventDispatcher\IEventDispatcher ;
2017-09-12 17:40:46 -04:00
use OCP\Federation\ICloudIdManager ;
2022-08-30 12:15:02 -04:00
use OCP\ICacheFactory ;
2017-09-12 17:40:46 -04:00
use OCP\IConfig ;
2017-12-16 02:46:03 -05:00
use OCP\IGroupManager ;
2021-07-09 02:00:03 -04:00
use OCP\IURLGenerator ;
2018-03-07 05:48:14 -05:00
use OCP\IUser ;
2021-07-09 02:00:03 -04:00
use OCP\IUserManager ;
2017-12-16 02:46:03 -05:00
use OCP\IUserSession ;
2023-11-23 04:22:34 -05:00
use OCP\Share\IShare ;
2017-09-12 17:40:46 -04:00
use Test\TestCase ;
2025-07-07 04:48:23 -04:00
use Test\Traits\EmailValidatorTrait ;
2017-09-12 17:40:46 -04:00
class MailPluginTest extends TestCase {
2025-07-07 04:48:23 -04:00
use EmailValidatorTrait ;
2024-08-23 09:10:27 -04:00
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
2017-09-12 17:40:46 -04:00
protected $config ;
2024-08-23 09:10:27 -04:00
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
2017-09-12 17:40:46 -04:00
protected $contactsManager ;
2024-08-23 09:10:27 -04:00
/** @var ICloudIdManager|\PHPUnit\Framework\MockObject\MockObject */
2017-09-12 17:40:46 -04:00
protected $cloudIdManager ;
2024-08-23 09:10:27 -04:00
/** @var MailPlugin */
2017-09-12 17:40:46 -04:00
protected $plugin ;
2024-08-23 09:10:27 -04:00
/** @var SearchResult */
2017-09-12 17:40:46 -04:00
protected $searchResult ;
2024-08-23 09:10:27 -04:00
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
2017-12-16 02:46:03 -05:00
protected $groupManager ;
2024-08-23 09:10:27 -04:00
/** @var KnownUserService|\PHPUnit\Framework\MockObject\MockObject */
2021-03-09 15:48:48 -05:00
protected $knownUserService ;
2024-08-23 09:10:27 -04:00
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
2017-12-16 02:46:03 -05:00
protected $userSession ;
2019-11-27 09:27:18 -05:00
protected function setUp () : void {
2017-09-12 17:40:46 -04:00
parent :: setUp ();
$this -> config = $this -> createMock ( IConfig :: class );
$this -> contactsManager = $this -> createMock ( IManager :: class );
2017-12-16 02:46:03 -05:00
$this -> groupManager = $this -> createMock ( IGroupManager :: class );
2021-03-09 15:48:48 -05:00
$this -> knownUserService = $this -> createMock ( KnownUserService :: class );
2017-12-16 02:46:03 -05:00
$this -> userSession = $this -> createMock ( IUserSession :: class );
2022-08-30 12:15:02 -04:00
$this -> cloudIdManager = new CloudIdManager (
2025-07-21 09:51:58 -04:00
$this -> createMock ( ICacheFactory :: class ),
$this -> createMock ( IEventDispatcher :: class ),
2022-08-30 12:15:02 -04:00
$this -> contactsManager ,
$this -> createMock ( IURLGenerator :: class ),
$this -> createMock ( IUserManager :: class ),
);
2020-11-16 11:56:44 -05:00
2017-09-12 17:40:46 -04:00
$this -> searchResult = new SearchResult ();
}
2025-04-07 21:05:30 -04:00
public function instantiatePlugin ( int $shareType ) {
2021-03-09 15:48:48 -05:00
$this -> plugin = new MailPlugin (
$this -> contactsManager ,
$this -> cloudIdManager ,
$this -> config ,
$this -> groupManager ,
$this -> knownUserService ,
2022-01-11 09:59:57 -05:00
$this -> userSession ,
2025-07-07 04:48:23 -04:00
$this -> getEmailValidatorWithStrictEmailCheck (),
2025-04-07 21:05:30 -04:00
[],
$shareType ,
2021-03-09 15:48:48 -05:00
);
2017-09-12 17:40:46 -04:00
}
/**
*
* @ param string $searchTerm
* @ param array $contacts
* @ param bool $shareeEnumeration
2025-03-31 15:04:21 -04:00
* @ param array $expectedResult
* @ param bool $expectedExactIdMatch
* @ param bool $expectedMoreResults
2025-04-05 05:55:17 -04:00
* @ param bool $validEmail
2017-09-12 17:40:46 -04:00
*/
2025-04-07 21:05:30 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchEmail')]
public function testSearchEmail ( $searchTerm , $contacts , $shareeEnumeration , $expectedResult , $expectedExactIdMatch , $expectedMoreResults , $validEmail ) : void {
2017-09-12 17:40:46 -04:00
$this -> config -> expects ( $this -> any ())
-> method ( 'getAppValue' )
-> willReturnCallback (
2020-04-10 08:19:56 -04:00
function ( $appName , $key , $default ) use ( $shareeEnumeration ) {
2017-09-12 17:40:46 -04:00
if ( $appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration' ) {
return $shareeEnumeration ? 'yes' : 'no' ;
}
return $default ;
}
);
2025-04-07 21:05:30 -04:00
$this -> instantiatePlugin ( IShare :: TYPE_EMAIL );
2017-09-12 17:40:46 -04:00
2018-03-07 05:48:14 -05:00
$currentUser = $this -> createMock ( IUser :: class );
$currentUser -> method ( 'getUID' )
-> willReturn ( 'current' );
$this -> userSession -> method ( 'getUser' )
-> willReturn ( $currentUser );
2017-09-12 17:40:46 -04:00
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
2020-11-16 11:56:44 -05:00
-> willReturnCallback ( function ( $search , $searchAttributes ) use ( $searchTerm , $contacts ) {
if ( $search === $searchTerm ) {
return $contacts ;
}
return [];
});
2017-09-12 17:40:46 -04:00
2018-02-06 12:11:44 -05:00
$moreResults = $this -> plugin -> search ( $searchTerm , 2 , 0 , $this -> searchResult );
2017-09-12 17:40:46 -04:00
$result = $this -> searchResult -> asArray ();
2025-03-31 15:04:21 -04:00
$this -> assertSame ( $expectedExactIdMatch , $this -> searchResult -> hasExactIdMatch ( new SearchResultType ( 'emails' )));
$this -> assertEquals ( $expectedResult , $result );
$this -> assertSame ( $expectedMoreResults , $moreResults );
2017-09-12 17:40:46 -04:00
}
2025-04-07 21:05:30 -04:00
public static function dataSearchEmail () : array {
2017-09-12 17:40:46 -04:00
return [
2018-10-30 05:18:58 -04:00
// data set 0
2022-01-11 09:59:57 -05:00
[ 'test' , [], true , [ 'emails' => [], 'exact' => [ 'emails' => []]], false , false , false ],
2018-10-30 05:18:58 -04:00
// data set 1
2022-01-11 09:59:57 -05:00
[ 'test' , [], false , [ 'emails' => [], 'exact' => [ 'emails' => []]], false , false , false ],
2018-10-30 05:18:58 -04:00
// data set 2
2017-09-12 17:40:46 -04:00
[
'test@remote.com' ,
[],
true ,
2020-06-24 10:49:16 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'uuid' => 'test@remote.com' , 'label' => 'test@remote.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@remote.com' ]]]]],
2017-09-12 17:40:46 -04:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
true ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 3
2017-09-12 17:40:46 -04:00
[ // no valid email address
'test@remote' ,
[],
true ,
[ 'emails' => [], 'exact' => [ 'emails' => []]],
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 4
2017-09-12 17:40:46 -04:00
[
'test@remote.com' ,
[],
false ,
2020-06-24 10:49:16 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'uuid' => 'test@remote.com' , 'label' => 'test@remote.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@remote.com' ]]]]],
2017-09-12 17:40:46 -04:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
true ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 5
2017-09-12 17:40:46 -04:00
[
'test' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User @ Localhost' ,
'EMAIL' => [
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
],
],
],
true ,
2025-03-26 06:22:57 -04:00
[ 'emails' => [[ 'uuid' => 'uid1' , 'name' => 'User @ Localhost' , 'type' => '' , 'label' => 'User @ Localhost (username@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'username@example.com' ]]], 'exact' => [ 'emails' => []]],
2017-09-12 17:40:46 -04:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 6
2017-09-12 17:40:46 -04:00
[
'test' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
2021-12-02 05:54:04 -05:00
'isLocalSystemBook' => true ,
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
false ,
[ 'emails' => [], 'exact' => [ 'emails' => []]],
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 7
2017-09-12 17:40:46 -04:00
[
'test@remote.com' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User3 @ example.com' ,
2017-09-12 17:40:46 -04:00
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User2 @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
],
],
],
true ,
2025-03-26 06:22:57 -04:00
[ 'emails' => [[ 'uuid' => 'uid1' , 'name' => 'User @ example.com' , 'type' => '' , 'label' => 'User @ example.com (username@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'username@example.com' ]]], 'exact' => [ 'emails' => [[ 'label' => 'test@remote.com' , 'uuid' => 'test@remote.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@remote.com' ]]]]],
2017-09-12 17:40:46 -04:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
true ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 8
2017-09-12 17:40:46 -04:00
[
'test@remote.com' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
2021-12-02 05:54:04 -05:00
'isLocalSystemBook' => true ,
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
false ,
2020-06-24 10:49:16 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'label' => 'test@remote.com' , 'uuid' => 'test@remote.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@remote.com' ]]]]],
2017-09-12 17:40:46 -04:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
true ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 9
2017-09-12 17:40:46 -04:00
[
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User3 @ example.com' ,
2017-09-12 17:40:46 -04:00
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User2 @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
],
],
],
true ,
2025-03-26 06:22:57 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'name' => 'User @ example.com' , 'uuid' => 'uid1' , 'type' => '' , 'label' => 'User @ example.com (username@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'username@example.com' ]]]]],
2017-09-12 17:40:46 -04:00
true ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 10
2017-09-12 17:40:46 -04:00
[
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User3 @ example.com' ,
2017-09-12 17:40:46 -04:00
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User2 @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2025-03-26 06:22:57 -04:00
'FN' => 'User @ example.com' ,
2017-09-12 17:40:46 -04:00
'EMAIL' => [
2025-03-26 06:22:57 -04:00
'username@example.com' ,
2017-09-12 17:40:46 -04:00
],
],
],
false ,
2025-03-26 06:22:57 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'name' => 'User @ example.com' , 'uuid' => 'uid1' , 'type' => '' , 'label' => 'User @ example.com (username@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'username@example.com' ]]]]],
2017-09-12 17:40:46 -04:00
true ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 11
2017-09-12 17:40:46 -04:00
// contact with space
[
'user name@localhost' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User Name @ Localhost' ,
'EMAIL' => [
'user name@localhost' ,
],
],
],
false ,
2025-03-26 06:22:57 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => []]],
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 12
2017-09-12 17:40:46 -04:00
// remote with space, no contact
[
'user space@remote.com' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User3 @ Localhost' ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
2021-12-02 05:54:04 -05:00
'isLocalSystemBook' => true ,
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
false ,
[ 'emails' => [], 'exact' => [ 'emails' => []]],
false ,
2018-03-07 05:57:08 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2017-09-12 17:40:46 -04:00
],
2018-10-30 05:18:58 -04:00
// data set 13
2025-04-07 21:05:30 -04:00
// Local user found by email => no result
2017-09-12 17:40:46 -04:00
[
'test@example.com' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2017-09-12 17:40:46 -04:00
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
]
],
false ,
2025-04-07 21:05:30 -04:00
[ 'exact' => []],
false ,
2017-09-12 17:40:46 -04:00
false ,
2022-01-11 09:59:57 -05:00
true ,
2018-03-07 05:48:14 -05:00
],
2018-10-30 05:18:58 -04:00
// data set 14
2018-03-07 05:48:14 -05:00
// Current local user found by email => no result
[
'test@example.com' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'current@localhost' ],
'isLocalSystemBook' => true ,
]
],
true ,
[ 'exact' => []],
false ,
false ,
2022-01-11 09:59:57 -05:00
true ,
2018-03-07 05:48:14 -05:00
],
2018-10-30 05:18:58 -04:00
// data set 15
2025-04-07 21:05:30 -04:00
// Several local users found by email => no result nor pagination
2018-03-07 05:48:14 -05:00
[
'test@example' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User1' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test1@localhost' ],
'isLocalSystemBook' => true ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User2' ,
'EMAIL' => [ 'test@example.de' ],
'CLOUD' => [ 'test2@localhost' ],
'isLocalSystemBook' => true ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User3' ,
'EMAIL' => [ 'test@example.org' ],
'CLOUD' => [ 'test3@localhost' ],
'isLocalSystemBook' => true ,
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid4' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User4' ,
'EMAIL' => [ 'test@example.net' ],
'CLOUD' => [ 'test4@localhost' ],
'isLocalSystemBook' => true ,
],
],
true ,
2025-04-07 21:05:30 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => []]],
false ,
2018-03-07 05:48:14 -05:00
false ,
2022-01-11 09:59:57 -05:00
false ,
2018-03-07 05:48:14 -05:00
],
2018-10-30 05:18:58 -04:00
// data set 16
2018-03-07 05:48:14 -05:00
// Pagination and "more results" for normal emails
[
'test@example' ,
[
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid1' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User1' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test1@localhost' ],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid2' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User2' ,
'EMAIL' => [ 'test@example.de' ],
'CLOUD' => [ 'test2@localhost' ],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid3' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User3' ,
'EMAIL' => [ 'test@example.org' ],
'CLOUD' => [ 'test3@localhost' ],
],
[
2018-10-30 05:18:58 -04:00
'UID' => 'uid4' ,
2018-03-07 05:48:14 -05:00
'FN' => 'User4' ,
'EMAIL' => [ 'test@example.net' ],
'CLOUD' => [ 'test4@localhost' ],
],
],
true ,
[ 'emails' => [
2020-06-24 10:49:16 -04:00
[ 'uuid' => 'uid1' , 'name' => 'User1' , 'type' => '' , 'label' => 'User1 (test@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@example.com' ]],
[ 'uuid' => 'uid2' , 'name' => 'User2' , 'type' => '' , 'label' => 'User2 (test@example.de)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@example.de' ]],
2018-03-07 05:48:14 -05:00
], 'exact' => [ 'emails' => []]],
false ,
true ,
2022-01-11 09:59:57 -05:00
false ,
2018-03-07 05:48:14 -05:00
],
2018-10-30 05:18:58 -04:00
// data set 17
// multiple email addresses with type
[
'User Name' ,
[
[
'UID' => 'uid3' ,
'FN' => 'User3' ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2' ,
'EMAIL' => [
],
],
[
'UID' => 'uid1' ,
'FN' => 'User Name' ,
'EMAIL' => [
2025-03-26 06:22:57 -04:00
[ 'type' => 'HOME' , 'value' => 'username@example.com' ],
[ 'type' => 'WORK' , 'value' => 'other@example.com' ],
2018-10-30 05:18:58 -04:00
],
],
],
false ,
[ 'emails' => [
], 'exact' => [ 'emails' => [
2025-03-26 06:22:57 -04:00
[ 'name' => 'User Name' , 'uuid' => 'uid1' , 'type' => 'HOME' , 'label' => 'User Name (username@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'username@example.com' ]],
[ 'name' => 'User Name' , 'uuid' => 'uid1' , 'type' => 'WORK' , 'label' => 'User Name (other@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'other@example.com' ]]
2018-10-30 05:18:58 -04:00
]]],
false ,
false ,
2022-01-11 09:59:57 -05:00
false ,
],
// data set 18
// idn email
[
'test@lölölölölölölöl.com' ,
[],
true ,
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'uuid' => 'test@lölölölölölölöl.com' , 'label' => 'test@lölölölölölölöl.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@lölölölölölölöl.com' ]]]]],
false ,
false ,
true ,
2018-10-30 05:18:58 -04:00
],
2017-09-12 17:40:46 -04:00
];
}
2017-12-16 02:46:03 -05:00
2025-04-07 21:05:30 -04:00
/**
*
* @ param string $searchTerm
* @ param array $contacts
* @ param bool $shareeEnumeration
* @ param array $expectedResult
* @ param bool $expectedExactIdMatch
* @ param bool $expectedMoreResults
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchUser')]
public function testSearchUser ( $searchTerm , $contacts , $shareeEnumeration , $expectedResult , $expectedExactIdMatch , $expectedMoreResults ) : void {
$this -> config -> expects ( $this -> any ())
-> method ( 'getAppValue' )
-> willReturnCallback (
function ( $appName , $key , $default ) use ( $shareeEnumeration ) {
if ( $appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration' ) {
return $shareeEnumeration ? 'yes' : 'no' ;
}
return $default ;
}
);
$this -> instantiatePlugin ( IShare :: TYPE_USER );
$currentUser = $this -> createMock ( IUser :: class );
$currentUser -> method ( 'getUID' )
-> willReturn ( 'current' );
$this -> userSession -> method ( 'getUser' )
-> willReturn ( $currentUser );
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
-> willReturnCallback ( function ( $search , $searchAttributes ) use ( $searchTerm , $contacts ) {
if ( $search === $searchTerm ) {
return $contacts ;
}
return [];
});
$moreResults = $this -> plugin -> search ( $searchTerm , 2 , 0 , $this -> searchResult );
$result = $this -> searchResult -> asArray ();
$this -> assertSame ( $expectedExactIdMatch , $this -> searchResult -> hasExactIdMatch ( new SearchResultType ( 'emails' )));
$this -> assertEquals ( $expectedResult , $result );
$this -> assertSame ( $expectedMoreResults , $moreResults );
}
public static function dataSearchUser () : array {
return [
// data set 0
[ 'test' , [], true , [ 'exact' => []], false , false ],
// data set 1
[ 'test' , [], false , [ 'exact' => []], false , false ],
// data set 2
[
'test@remote.com' ,
[],
true ,
[ 'exact' => []],
false ,
false ,
],
// data set 3
[
'test@remote.com' ,
[],
false ,
[ 'exact' => []],
false ,
false ,
],
// data set 4
[
'test' ,
[
[
'UID' => 'uid3' ,
'FN' => 'User3 @ Localhost' ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
'UID' => 'uid1' ,
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
true ,
[ 'exact' => []],
false ,
false ,
],
// data set 5
[
'test' ,
[
[
'UID' => 'uid3' ,
'FN' => 'User3 @ Localhost' ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
'isLocalSystemBook' => true ,
'UID' => 'uid1' ,
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
false ,
[ 'exact' => []],
false ,
false ,
],
// data set 6
[
'test@remote.com' ,
[
[
'UID' => 'uid3' ,
'FN' => 'User3 @ Localhost' ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
'UID' => 'uid1' ,
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
true ,
[ 'exact' => []],
false ,
false ,
],
// data set 7
[
'username@localhost' ,
[
[
'UID' => 'uid3' ,
'FN' => 'User3 @ Localhost' ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2 @ Localhost' ,
'EMAIL' => [
],
],
[
'UID' => 'uid1' ,
'FN' => 'User @ Localhost' ,
'EMAIL' => [
'username@localhost' ,
],
],
],
true ,
[ 'exact' => []],
false ,
false ,
],
// data set 8
// Local user found by email
[
'test@example.com' ,
[
[
'UID' => 'uid1' ,
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
]
],
false ,
[ 'users' => [], 'exact' => [ 'users' => [[ 'uuid' => 'uid1' , 'name' => 'User' , 'label' => 'User (test@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_USER , 'shareWith' => 'test' ], 'shareWithDisplayNameUnique' => 'test@example.com' ]]]],
true ,
false ,
],
// data set 9
// Current local user found by email => no result
[
'test@example.com' ,
[
[
'UID' => 'uid1' ,
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'current@localhost' ],
'isLocalSystemBook' => true ,
]
],
true ,
[ 'exact' => []],
false ,
false ,
],
// data set 10
// Pagination and "more results" for user matches by emails
[
'test@example' ,
[
[
'UID' => 'uid1' ,
'FN' => 'User1' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test1@localhost' ],
'isLocalSystemBook' => true ,
],
[
'UID' => 'uid2' ,
'FN' => 'User2' ,
'EMAIL' => [ 'test@example.de' ],
'CLOUD' => [ 'test2@localhost' ],
'isLocalSystemBook' => true ,
],
[
'UID' => 'uid3' ,
'FN' => 'User3' ,
'EMAIL' => [ 'test@example.org' ],
'CLOUD' => [ 'test3@localhost' ],
'isLocalSystemBook' => true ,
],
[
'UID' => 'uid4' ,
'FN' => 'User4' ,
'EMAIL' => [ 'test@example.net' ],
'CLOUD' => [ 'test4@localhost' ],
'isLocalSystemBook' => true ,
],
],
true ,
[ 'users' => [
[ 'uuid' => 'uid1' , 'name' => 'User1' , 'label' => 'User1 (test@example.com)' , 'value' => [ 'shareType' => IShare :: TYPE_USER , 'shareWith' => 'test1' ], 'shareWithDisplayNameUnique' => 'test@example.com' ],
[ 'uuid' => 'uid2' , 'name' => 'User2' , 'label' => 'User2 (test@example.de)' , 'value' => [ 'shareType' => IShare :: TYPE_USER , 'shareWith' => 'test2' ], 'shareWithDisplayNameUnique' => 'test@example.de' ],
], 'exact' => [ 'users' => []]],
false ,
true ,
],
// data set 11
// Pagination and "more results" for normal emails
[
'test@example' ,
[
[
'UID' => 'uid1' ,
'FN' => 'User1' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test1@localhost' ],
],
[
'UID' => 'uid2' ,
'FN' => 'User2' ,
'EMAIL' => [ 'test@example.de' ],
'CLOUD' => [ 'test2@localhost' ],
],
[
'UID' => 'uid3' ,
'FN' => 'User3' ,
'EMAIL' => [ 'test@example.org' ],
'CLOUD' => [ 'test3@localhost' ],
],
[
'UID' => 'uid4' ,
'FN' => 'User4' ,
'EMAIL' => [ 'test@example.net' ],
'CLOUD' => [ 'test4@localhost' ],
],
],
true ,
[ 'exact' => []],
false ,
false ,
],
];
}
2017-12-16 02:46:03 -05:00
/**
*
* @ param string $searchTerm
* @ param array $contacts
2025-03-31 15:04:21 -04:00
* @ param array $expectedResult
* @ param bool $expectedExactIdMatch
* @ param bool $expectedMoreResults
2025-04-05 05:55:17 -04:00
* @ param array $userToGroupMapping
* @ param bool $validEmail
2017-12-16 02:46:03 -05:00
*/
2025-04-07 21:05:30 -04:00
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchEmailGroupsOnly')]
public function testSearchEmailGroupsOnly ( $searchTerm , $contacts , $expectedResult , $expectedExactIdMatch , $expectedMoreResults , $userToGroupMapping , $validEmail ) : void {
2017-12-16 02:46:03 -05:00
$this -> config -> expects ( $this -> any ())
-> method ( 'getAppValue' )
-> willReturnCallback (
2020-04-09 07:53:40 -04:00
function ( $appName , $key , $default ) {
2017-12-16 02:46:03 -05:00
if ( $appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration' ) {
return 'yes' ;
2020-04-10 04:35:09 -04:00
} elseif ( $appName === 'core' && $key === 'shareapi_only_share_with_group_members' ) {
2017-12-16 02:46:03 -05:00
return 'yes' ;
}
return $default ;
}
);
2025-04-07 21:05:30 -04:00
$this -> instantiatePlugin ( IShare :: TYPE_EMAIL );
2017-12-16 02:46:03 -05:00
2025-06-30 10:56:59 -04:00
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
2017-12-16 02:46:03 -05:00
$currentUser = $this -> createMock ( '\OCP\IUser' );
$currentUser -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'currentUser' );
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
2020-11-16 11:56:44 -05:00
-> willReturnCallback ( function ( $search , $searchAttributes ) use ( $searchTerm , $contacts ) {
if ( $search === $searchTerm ) {
return $contacts ;
}
return [];
});
2017-12-16 02:46:03 -05:00
$this -> userSession -> expects ( $this -> any ())
-> method ( 'getUser' )
-> willReturn ( $currentUser );
$this -> groupManager -> expects ( $this -> any ())
-> method ( 'getUserGroupIds' )
2025-06-12 12:31:58 -04:00
-> willReturnCallback ( function ( IUser $user ) use ( $userToGroupMapping ) {
2017-12-16 02:46:03 -05:00
return $userToGroupMapping [ $user -> getUID ()];
});
$this -> groupManager -> expects ( $this -> any ())
-> method ( 'isInGroup' )
2020-04-09 07:53:40 -04:00
-> willReturnCallback ( function ( $userId , $group ) use ( $userToGroupMapping ) {
2017-12-16 02:46:03 -05:00
return in_array ( $group , $userToGroupMapping [ $userId ]);
});
2018-02-06 12:11:44 -05:00
$moreResults = $this -> plugin -> search ( $searchTerm , 2 , 0 , $this -> searchResult );
2017-12-16 02:46:03 -05:00
$result = $this -> searchResult -> asArray ();
2025-03-31 15:04:21 -04:00
$this -> assertSame ( $expectedExactIdMatch , $this -> searchResult -> hasExactIdMatch ( new SearchResultType ( 'emails' )));
$this -> assertEquals ( $expectedResult , $result );
$this -> assertSame ( $expectedMoreResults , $moreResults );
2017-12-16 02:46:03 -05:00
}
2025-04-07 21:05:30 -04:00
public static function dataSearchEmailGroupsOnly () : array {
2017-12-16 02:46:03 -05:00
return [
// The user `User` can share with the current user
[
'test' ,
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
2025-04-07 21:05:30 -04:00
'UID' => 'User' ,
2017-12-16 02:46:03 -05:00
]
],
2025-04-07 21:05:30 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => []]],
2017-12-16 02:46:03 -05:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2017-12-16 02:46:03 -05:00
[
2024-08-23 09:10:27 -04:00
'currentUser' => [ 'group1' ],
2025-04-07 21:05:30 -04:00
'User' => [ 'group1' ],
2022-01-11 09:59:57 -05:00
],
false ,
2017-12-16 02:46:03 -05:00
],
// The user `User` cannot share with the current user
[
2020-04-09 03:22:29 -04:00
'test' ,
2017-12-16 02:46:03 -05:00
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
2025-04-07 21:05:30 -04:00
'UID' => 'User' ,
2017-12-16 02:46:03 -05:00
]
],
2020-10-05 09:12:57 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => []]],
2017-12-16 02:46:03 -05:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2017-12-16 02:46:03 -05:00
[
2024-08-23 09:10:27 -04:00
'currentUser' => [ 'group1' ],
2025-04-07 21:05:30 -04:00
'User' => [ 'group2' ],
2022-01-11 09:59:57 -05:00
],
false ,
2017-12-16 02:46:03 -05:00
],
// The user `User` cannot share with the current user, but there is an exact match on the e-mail address -> share by e-mail
[
2020-04-09 03:22:29 -04:00
'test@example.com' ,
2017-12-16 02:46:03 -05:00
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
2025-04-07 21:05:30 -04:00
'UID' => 'User' ,
2017-12-16 02:46:03 -05:00
]
],
2025-03-31 15:18:43 -04:00
[ 'emails' => [], 'exact' => [ 'emails' => [[ 'label' => 'test@example.com' , 'uuid' => 'test@example.com' , 'value' => [ 'shareType' => IShare :: TYPE_EMAIL , 'shareWith' => 'test@example.com' ]]]]],
2017-12-16 02:46:03 -05:00
false ,
2018-03-07 05:57:08 -05:00
false ,
2017-12-16 02:46:03 -05:00
[
2024-08-23 09:10:27 -04:00
'currentUser' => [ 'group1' ],
2025-04-07 21:05:30 -04:00
'User' => [ 'group2' ],
2022-01-11 09:59:57 -05:00
],
true ,
2017-12-16 02:46:03 -05:00
]
];
}
2025-04-07 21:05:30 -04:00
/**
*
* @ param string $searchTerm
* @ param array $contacts
* @ param array $expectedResult
* @ param bool $expectedExactIdMatch
* @ param bool $expectedMoreResults
* @ param array $userToGroupMapping
*/
#[\PHPUnit\Framework\Attributes\DataProvider('dataSearchUserGroupsOnly')]
public function testSearchUserGroupsOnly ( $searchTerm , $contacts , $expectedResult , $expectedExactIdMatch , $expectedMoreResults , $userToGroupMapping ) : void {
$this -> config -> expects ( $this -> any ())
-> method ( 'getAppValue' )
-> willReturnCallback (
function ( $appName , $key , $default ) {
if ( $appName === 'core' && $key === 'shareapi_allow_share_dialog_user_enumeration' ) {
return 'yes' ;
} elseif ( $appName === 'core' && $key === 'shareapi_only_share_with_group_members' ) {
return 'yes' ;
}
return $default ;
}
);
$this -> instantiatePlugin ( IShare :: TYPE_USER );
2025-11-17 05:58:02 -05:00
/** @var IUser|\PHPUnit\Framework\MockObject\MockObject */
2025-04-07 21:05:30 -04:00
$currentUser = $this -> createMock ( '\OCP\IUser' );
$currentUser -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'currentUser' );
$this -> contactsManager -> expects ( $this -> any ())
-> method ( 'search' )
-> willReturnCallback ( function ( $search , $searchAttributes ) use ( $searchTerm , $contacts ) {
if ( $search === $searchTerm ) {
return $contacts ;
}
return [];
});
$this -> userSession -> expects ( $this -> any ())
-> method ( 'getUser' )
-> willReturn ( $currentUser );
$this -> groupManager -> expects ( $this -> any ())
-> method ( 'getUserGroupIds' )
2025-11-17 05:58:02 -05:00
-> willReturnCallback ( function ( IUser $user ) use ( $userToGroupMapping ) {
2025-04-07 21:05:30 -04:00
return $userToGroupMapping [ $user -> getUID ()];
});
$this -> groupManager -> expects ( $this -> any ())
-> method ( 'isInGroup' )
-> willReturnCallback ( function ( $userId , $group ) use ( $userToGroupMapping ) {
return in_array ( $group , $userToGroupMapping [ $userId ]);
});
$moreResults = $this -> plugin -> search ( $searchTerm , 2 , 0 , $this -> searchResult );
$result = $this -> searchResult -> asArray ();
$this -> assertSame ( $expectedExactIdMatch , $this -> searchResult -> hasExactIdMatch ( new SearchResultType ( 'emails' )));
$this -> assertEquals ( $expectedResult , $result );
$this -> assertSame ( $expectedMoreResults , $moreResults );
}
public static function dataSearchUserGroupsOnly () : array {
return [
// The user `User` can share with the current user
[
'test' ,
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
'UID' => 'User' ,
]
],
[ 'users' => [[ 'label' => 'User (test@example.com)' , 'uuid' => 'User' , 'name' => 'User' , 'value' => [ 'shareType' => IShare :: TYPE_USER , 'shareWith' => 'test' ], 'shareWithDisplayNameUnique' => 'test@example.com' ,]], 'exact' => [ 'users' => []]],
false ,
false ,
[
'currentUser' => [ 'group1' ],
'User' => [ 'group1' ],
],
],
// The user `User` cannot share with the current user
[
'test' ,
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
'UID' => 'User' ,
]
],
[ 'exact' => []],
false ,
false ,
[
'currentUser' => [ 'group1' ],
'User' => [ 'group2' ],
],
],
// The user `User` cannot share with the current user, but there is an exact match on the e-mail address -> share by e-mail
[
'test@example.com' ,
[
[
'FN' => 'User' ,
'EMAIL' => [ 'test@example.com' ],
'CLOUD' => [ 'test@localhost' ],
'isLocalSystemBook' => true ,
'UID' => 'User' ,
]
],
[ 'exact' => []],
false ,
false ,
[
'currentUser' => [ 'group1' ],
'User' => [ 'group2' ],
],
]
];
}
2017-09-12 17:40:46 -04:00
}