From 75ea1a1e21764af82578e23d67ca80e75904ab9b Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Fri, 15 Sep 2023 13:21:46 +0200 Subject: [PATCH] Add missing PHPUnit tests for hook handlers Bug: T345620 Change-Id: I98283917644116da2a9668a5618bac086c863227 --- tests/phpunit/RevisionSliderHooksTest.php | 114 ++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 tests/phpunit/RevisionSliderHooksTest.php diff --git a/tests/phpunit/RevisionSliderHooksTest.php b/tests/phpunit/RevisionSliderHooksTest.php new file mode 100644 index 00000000..f3169f2a --- /dev/null +++ b/tests/phpunit/RevisionSliderHooksTest.php @@ -0,0 +1,114 @@ +newDiffEngine( null, $output ); + + // Act + $this->newInstance()->onDifferenceEngineViewHeader( $diffEngine ); + } + + public function testShouldNotLoadOnMobileDiff() { + // Arrange + $title = $this->createMock( Title::class ); + $title->method( 'isSpecial' ) + ->with( 'MobileDiff' ) + ->willReturn( true ); + + $output = $this->createMock( OutputPage::class ); + $output->method( 'getTitle' ) + ->willReturn( $title ); + + $revision = $this->createMock( RevisionRecord::class ); + $diffEngine = $this->newDiffEngine( $revision, $output ); + + // Assert + $output->expects( $this->never() ) + ->method( 'addModules' ); + + // Act + $this->newInstance()->onDifferenceEngineViewHeader( $diffEngine ); + } + + public function testShouldNotLoadWhenUserIsLoggedInAndDisabledExtension() { + // Arrange + $options = [ 'revisionslider-disable' => true ]; + $user = $this->createMock( User::class ); + $user->method( 'isNamed' ) + ->willReturn( true ); + + $output = $this->createMock( OutputPage::class ); + $output->method( 'getTitle' ) + ->willReturn( $this->createMock( Title::class ) ); + + $revision = $this->createMock( RevisionRecord::class ); + $diffEngine = $this->newDiffEngine( $revision, $output, $user ); + + // Assert + $output->expects( $this->never() ) + ->method( 'addModules' ); + + // Act + $this->newInstance( $options )->onDifferenceEngineViewHeader( $diffEngine ); + } + + public function testOnGetPreferences() { + // Arrange + $user = $this->createMock( User::class ); + $preferences = []; + + // Act + $this->newInstance()->onGetPreferences( $user, $preferences ); + + // Assert + $this->assertArrayHasKey( 'revisionslider-disable', $preferences ); + $item = $preferences['revisionslider-disable']; + $this->assertSame( 'toggle', $item['type'] ); + $this->assertSame( 'revisionslider-preference-disable', $item['label-message'] ); + $this->assertSame( 'rendering/diffs', $item['section'] ); + } + + public function newInstance( array $options = [] ): RevisionSliderHooks { + $configFactory = $this->createMock( ConfigFactory::class ); + $configFactory->method( 'makeConfig' ) + ->willReturn( new HashConfig() ); + + return new RevisionSliderHooks( + $configFactory, + new StaticUserOptionsLookup( [], $options ), + new NullStatsdDataFactory() + ); + } + + private function newDiffEngine( + ?RevisionRecord $revision, + ?OutputPage $output, + ?User $user = null + ): DifferenceEngine { + $diffEngine = $this->createMock( DifferenceEngine::class ); + $diffEngine->method( 'getOldRevision' ) + ->willReturn( $revision ); + $diffEngine->method( 'getNewRevision' ) + ->willReturn( $revision ); + $diffEngine->expects( $output ? $this->atLeastOnce() : $this->never() ) + ->method( 'getOutput' ) + ->willReturn( $output ); + $diffEngine->expects( $user ? $this->once() : $this->never() ) + ->method( 'getUser' ) + ->willReturn( $user ); + return $diffEngine; + } + +}