2017-02-07 13:49:01 +03:00
< ? php
/**
* @ copyright Copyright ( c ) 2017 Joas Schilling < coding @ schilljs . com >
*
2017-11-06 17:56:42 +03:00
* @ author Joas Schilling < coding @ schilljs . com >
2019-12-03 21:57:53 +03:00
* @ author Roeland Jago Douma < roeland @ famdouma . nl >
2017-11-06 17:56:42 +03:00
*
2017-02-07 13:49:01 +03:00
* @ license GNU AGPL version 3 or any later version
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation , either version 3 of the
* License , or ( at your option ) any later version .
*
* This program 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
2019-12-03 21:57:53 +03:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2017-02-07 13:49:01 +03:00
*
*/
namespace OCA\Files\Tests\Activity ;
use OCA\Files\Activity\Provider ;
use OCP\Activity\IEvent ;
use OCP\Activity\IEventMerger ;
use OCP\Activity\IManager ;
2020-02-07 12:51:22 +03:00
use OCP\Contacts\IManager as IContactsManager ;
use OCP\Federation\ICloudId ;
use OCP\Federation\ICloudIdManager ;
2018-02-28 18:04:45 +03:00
use OCP\Files\IRootFolder ;
2017-02-07 13:49:01 +03:00
use OCP\IURLGenerator ;
2017-02-07 14:01:28 +03:00
use OCP\IUser ;
2017-02-07 13:49:01 +03:00
use OCP\IUserManager ;
use OCP\L10N\IFactory ;
2020-02-07 12:51:22 +03:00
use PHPUnit\Framework\MockObject\MockObject ;
2017-02-07 13:49:01 +03:00
use Test\TestCase ;
/**
* Class ProviderTest
*
* @ package OCA\Files\Tests\Activity
*/
class ProviderTest extends TestCase {
2020-02-07 12:51:22 +03:00
/** @var IFactory|MockObject */
2017-02-07 13:49:01 +03:00
protected $l10nFactory ;
2020-02-07 12:51:22 +03:00
/** @var IURLGenerator|MockObject */
2017-02-07 13:49:01 +03:00
protected $url ;
2020-02-07 12:51:22 +03:00
/** @var IManager|MockObject */
2017-02-07 13:49:01 +03:00
protected $activityManager ;
2020-02-07 12:51:22 +03:00
/** @var IUserManager|MockObject */
2017-02-07 13:49:01 +03:00
protected $userManager ;
2020-02-07 12:51:22 +03:00
/** @var IRootFolder|MockObject */
2018-02-28 18:04:45 +03:00
protected $rootFolder ;
2020-02-07 12:51:22 +03:00
/** @var ICloudIdManager|MockObject */
protected $cloudIdManager ;
/** @var IContactsManager|MockObject */
protected $contactsManager ;
/** @var IEventMerger|MockObject */
2017-02-07 13:49:01 +03:00
protected $eventMerger ;
2019-11-27 17:27:18 +03:00
protected function setUp () : void {
2017-02-07 13:49:01 +03:00
parent :: setUp ();
$this -> l10nFactory = $this -> createMock ( IFactory :: class );
$this -> url = $this -> createMock ( IURLGenerator :: class );
$this -> activityManager = $this -> createMock ( IManager :: class );
$this -> userManager = $this -> createMock ( IUserManager :: class );
2018-02-28 18:04:45 +03:00
$this -> rootFolder = $this -> createMock ( IRootFolder :: class );
2020-02-07 12:51:22 +03:00
$this -> cloudIdManager = $this -> createMock ( ICloudIdManager :: class );
$this -> contactsManager = $this -> createMock ( IContactsManager :: class );
2017-02-07 13:49:01 +03:00
$this -> eventMerger = $this -> createMock ( IEventMerger :: class );
}
/**
* @ param string [] $methods
2020-02-07 12:51:22 +03:00
* @ return Provider | MockObject
2017-02-07 13:49:01 +03:00
*/
protected function getProvider ( array $methods = []) {
if ( ! empty ( $methods )) {
return $this -> getMockBuilder ( Provider :: class )
-> setConstructorArgs ([
$this -> l10nFactory ,
$this -> url ,
$this -> activityManager ,
$this -> userManager ,
2018-02-28 18:04:45 +03:00
$this -> rootFolder ,
2020-02-07 12:51:22 +03:00
$this -> cloudIdManager ,
$this -> contactsManager ,
2017-02-07 13:49:01 +03:00
$this -> eventMerger ,
])
-> setMethods ( $methods )
-> getMock ();
}
return new Provider (
$this -> l10nFactory ,
$this -> url ,
$this -> activityManager ,
$this -> userManager ,
2018-02-28 18:04:45 +03:00
$this -> rootFolder ,
2020-02-07 12:51:22 +03:00
$this -> cloudIdManager ,
$this -> contactsManager ,
2017-02-07 13:49:01 +03:00
$this -> eventMerger
);
}
public function dataGetFile () {
return [
[[ 42 => '/FortyTwo.txt' ], null , '42' , 'FortyTwo.txt' , 'FortyTwo.txt' ],
[[ '23' => '/Twenty/Three.txt' ], null , '23' , 'Three.txt' , 'Twenty/Three.txt' ],
2020-02-07 12:51:22 +03:00
[ '/Foo/Bar.txt' , 128 , 128 , 'Bar.txt' , 'Foo/Bar.txt' ], // Legacy from ownCloud 8.2 and before
2017-02-07 13:49:01 +03:00
];
}
/**
* @ dataProvider dataGetFile
* @ param mixed $parameter
* @ param mixed $eventId
* @ param int $id
* @ param string $name
* @ param string $path
*/
public function testGetFile ( $parameter , $eventId , $id , $name , $path ) {
$provider = $this -> getProvider ();
if ( $eventId !== null ) {
$event = $this -> createMock ( IEvent :: class );
$event -> expects ( $this -> once ())
-> method ( 'getObjectId' )
-> willReturn ( $eventId );
} else {
$event = null ;
}
$this -> url -> expects ( $this -> once ())
-> method ( 'linkToRouteAbsolute' )
-> with ( 'files.viewcontroller.showFile' , [ 'fileid' => $id ])
-> willReturn ( 'link-' . $id );
$result = self :: invokePrivate ( $provider , 'getFile' , [ $parameter , $event ]);
$this -> assertSame ( 'file' , $result [ 'type' ]);
$this -> assertSame ( $id , $result [ 'id' ]);
$this -> assertSame ( $name , $result [ 'name' ]);
$this -> assertSame ( $path , $result [ 'path' ]);
$this -> assertSame ( 'link-' . $id , $result [ 'link' ]);
}
2020-02-07 12:51:22 +03:00
2017-02-07 13:49:01 +03:00
public function testGetFileThrows () {
2019-11-27 17:27:18 +03:00
$this -> expectException ( \InvalidArgumentException :: class );
2017-02-07 13:49:01 +03:00
$provider = $this -> getProvider ();
self :: invokePrivate ( $provider , 'getFile' , [ '/Foo/Bar.txt' , null ]);
}
2017-02-07 14:01:28 +03:00
public function dataGetUser () {
return [
2020-02-07 12:51:22 +03:00
[ 'test' , 'Test user' , null , [ 'type' => 'user' , 'id' => 'test' , 'name' => 'Test user' ]],
[ 'test@http://localhost' , null , [ 'user' => 'test' , 'displayId' => 'test@localhost' , 'remote' => 'localhost' , 'name' => null ], [ 'type' => 'user' , 'id' => 'test' , 'name' => 'test@localhost' , 'server' => 'localhost' ]],
[ 'test@http://localhost' , null , [ 'user' => 'test' , 'displayId' => 'test@localhost' , 'remote' => 'localhost' , 'name' => 'Remote user' ], [ 'type' => 'user' , 'id' => 'test' , 'name' => 'Remote user (test@localhost)' , 'server' => 'localhost' ]],
[ 'test' , null , null , [ 'type' => 'user' , 'id' => 'test' , 'name' => 'test' ]],
2017-02-07 14:01:28 +03:00
];
}
/**
* @ dataProvider dataGetUser
* @ param string $uid
2020-02-07 12:51:22 +03:00
* @ param string | null $userDisplayName
* @ param array | null $cloudIdData
* @ param array $expected
2017-02-07 14:01:28 +03:00
*/
2020-02-07 12:51:22 +03:00
public function testGetUser ( string $uid , ? string $userDisplayName , ? array $cloudIdData , array $expected ) : void {
2017-02-07 14:01:28 +03:00
$provider = $this -> getProvider ();
2020-02-07 12:51:22 +03:00
if ( $userDisplayName !== null ) {
2017-02-07 14:01:28 +03:00
$user = $this -> createMock ( IUser :: class );
2020-02-07 12:51:22 +03:00
$user -> expects ( $this -> once ())
-> method ( 'getUID' )
-> willReturn ( $uid );
2017-02-07 14:01:28 +03:00
$user -> expects ( $this -> once ())
-> method ( 'getDisplayName' )
2020-02-07 12:51:22 +03:00
-> willReturn ( $userDisplayName );
2017-02-07 14:01:28 +03:00
$this -> userManager -> expects ( $this -> once ())
-> method ( 'get' )
-> with ( $uid )
-> willReturn ( $user );
2020-02-07 12:51:22 +03:00
}
if ( $cloudIdData !== null ) {
$this -> cloudIdManager -> expects ( $this -> once ())
-> method ( 'isValidCloudId' )
-> willReturn ( true );
$cloudId = $this -> createMock ( ICloudId :: class );
$cloudId -> expects ( $this -> once ())
-> method ( 'getUser' )
-> willReturn ( $cloudIdData [ 'user' ]);
$cloudId -> expects ( $this -> once ())
-> method ( 'getDisplayId' )
-> willReturn ( $cloudIdData [ 'displayId' ]);
$cloudId -> expects ( $this -> once ())
-> method ( 'getRemote' )
-> willReturn ( $cloudIdData [ 'remote' ]);
$this -> cloudIdManager -> expects ( $this -> once ())
-> method ( 'resolveCloudId' )
2017-02-07 14:01:28 +03:00
-> with ( $uid )
2020-02-07 12:51:22 +03:00
-> willReturn ( $cloudId );
if ( $cloudIdData [ 'name' ] !== null ) {
$this -> contactsManager -> expects ( $this -> once ())
-> method ( 'search' )
-> with ( $cloudIdData [ 'displayId' ], [ 'CLOUD' ])
-> willReturn ([
[
'CLOUD' => $cloudIdData [ 'displayId' ],
'FN' => $cloudIdData [ 'name' ],
]
]);
} else {
$this -> contactsManager -> expects ( $this -> once ())
-> method ( 'search' )
-> with ( $cloudIdData [ 'displayId' ], [ 'CLOUD' ])
-> willReturn ([]);
}
2017-02-07 14:01:28 +03:00
}
2020-02-07 12:51:22 +03:00
$result = self :: invokePrivate ( $provider , 'getUser' , [ $uid ]);
$this -> assertEquals ( $expected , $result );
2017-02-07 14:01:28 +03:00
}
2017-02-07 13:49:01 +03:00
}