|  | 
| 26 | 26 |  */ | 
| 27 | 27 | namespace OCA\UserStatus\Tests\Service; | 
| 28 | 28 | 
 | 
|  | 29 | +use Doctrine\DBAL\Exception\UniqueConstraintViolationException; | 
|  | 30 | +use OC\DB\Exceptions\DbalException; | 
| 29 | 31 | use OCA\UserStatus\Db\UserStatus; | 
| 30 | 32 | use OCA\UserStatus\Db\UserStatusMapper; | 
| 31 | 33 | use OCA\UserStatus\Exception\InvalidClearAtException; | 
|  | 
| 38 | 40 | use OCA\UserStatus\Service\StatusService; | 
| 39 | 41 | use OCP\AppFramework\Db\DoesNotExistException; | 
| 40 | 42 | use OCP\AppFramework\Utility\ITimeFactory; | 
|  | 43 | +use OCP\DB\Exception; | 
| 41 | 44 | use OCP\IConfig; | 
| 42 | 45 | use OCP\UserStatus\IUserStatus; | 
| 43 | 46 | use Test\TestCase; | 
| @@ -723,53 +726,36 @@ public function testCleanStatusCleanedAlready(): void { | 
| 723 | 726 | 		parent::invokePrivate($this->service, 'cleanStatus', [$status]); | 
| 724 | 727 | 	} | 
| 725 | 728 | 
 | 
| 726 |  | -	public function testBackupWorkingHasBackupAlready() { | 
| 727 |  | -		$status = new UserStatus(); | 
| 728 |  | -		$status->setStatus(IUserStatus::ONLINE); | 
| 729 |  | -		$status->setStatusTimestamp(1337); | 
| 730 |  | -		$status->setIsUserDefined(true); | 
| 731 |  | -		$status->setMessageId('meeting'); | 
| 732 |  | -		$status->setUserId('john'); | 
| 733 |  | -		$status->setIsBackup(true); | 
| 734 |  | - | 
|  | 729 | +	public function testBackupWorkingHasBackupAlready(): void { | 
|  | 730 | +		$p = $this->createMock(UniqueConstraintViolationException::class); | 
|  | 731 | +		$e = DbalException::wrap($p); | 
| 735 | 732 | 		$this->mapper->expects($this->once()) | 
| 736 |  | -			->method('findByUserId') | 
| 737 |  | -			->with('john', true) | 
| 738 |  | -			->willReturn($status); | 
|  | 733 | +			->method('createBackupStatus') | 
|  | 734 | +			->with('john') | 
|  | 735 | +			->willThrowException($e); | 
| 739 | 736 | 
 | 
| 740 |  | -		$this->service->backupCurrentStatus('john'); | 
|  | 737 | +		$this->assertFalse($this->service->backupCurrentStatus('john')); | 
| 741 | 738 | 	} | 
| 742 | 739 | 
 | 
| 743 |  | -	public function testBackup() { | 
| 744 |  | -		$currentStatus = new UserStatus(); | 
| 745 |  | -		$currentStatus->setStatus(IUserStatus::ONLINE); | 
| 746 |  | -		$currentStatus->setStatusTimestamp(1337); | 
| 747 |  | -		$currentStatus->setIsUserDefined(true); | 
| 748 |  | -		$currentStatus->setMessageId('meeting'); | 
| 749 |  | -		$currentStatus->setUserId('john'); | 
| 750 |  | - | 
| 751 |  | -		$this->mapper->expects($this->at(0)) | 
| 752 |  | -			->method('findByUserId') | 
| 753 |  | -			->with('john', true) | 
| 754 |  | -			->willThrowException(new DoesNotExistException('')); | 
| 755 |  | -		$this->mapper->expects($this->at(1)) | 
| 756 |  | -			->method('findByUserId') | 
| 757 |  | -			->with('john', false) | 
| 758 |  | -			->willReturn($currentStatus); | 
|  | 740 | +	public function testBackupThrowsOther(): void { | 
|  | 741 | +		$e = new Exception('', Exception::REASON_CONNECTION_LOST); | 
|  | 742 | +		$this->mapper->expects($this->once()) | 
|  | 743 | +			->method('createBackupStatus') | 
|  | 744 | +			->with('john') | 
|  | 745 | +			->willThrowException($e); | 
| 759 | 746 | 
 | 
| 760 |  | -		$newBackupStatus = new UserStatus(); | 
| 761 |  | -		$newBackupStatus->setStatus(IUserStatus::ONLINE); | 
| 762 |  | -		$newBackupStatus->setStatusTimestamp(1337); | 
| 763 |  | -		$newBackupStatus->setIsUserDefined(true); | 
| 764 |  | -		$newBackupStatus->setMessageId('meeting'); | 
| 765 |  | -		$newBackupStatus->setUserId('_john'); | 
| 766 |  | -		$newBackupStatus->setIsBackup(true); | 
|  | 747 | +		$this->expectException(Exception::class); | 
|  | 748 | +		$this->service->backupCurrentStatus('john'); | 
|  | 749 | +	} | 
| 767 | 750 | 
 | 
|  | 751 | +	public function testBackup(): void { | 
|  | 752 | +		$e = new Exception('', Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION); | 
| 768 | 753 | 		$this->mapper->expects($this->once()) | 
| 769 |  | -			->method('update') | 
| 770 |  | -			->with($newBackupStatus); | 
|  | 754 | +			->method('createBackupStatus') | 
|  | 755 | +			->with('john') | 
|  | 756 | +			->willReturn(true); | 
| 771 | 757 | 
 | 
| 772 |  | -		$this->service->backupCurrentStatus('john'); | 
|  | 758 | +		$this->assertTrue($this->service->backupCurrentStatus('john')); | 
| 773 | 759 | 	} | 
| 774 | 760 | 
 | 
| 775 | 761 | 	public function testRevertMultipleUserStatus(): void { | 
|  | 
0 commit comments