Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/185793

Change subject: Add basic tests
......................................................................

Add basic tests

Change-Id: I7b8b3ce3f44e5366ed9ed28fff8e83a531cddd5e
---
M googleAnalytics.hooks.php
M googleAnalytics.php
A tests/GoogleAnalyticsHooksTest.php
3 files changed, 115 insertions(+), 0 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/googleAnalytics 
refs/changes/93/185793/1

diff --git a/googleAnalytics.hooks.php b/googleAnalytics.hooks.php
index dfd90a5..88d9cc2 100644
--- a/googleAnalytics.hooks.php
+++ b/googleAnalytics.hooks.php
@@ -58,4 +58,22 @@
                return true;
        }
 
+       public static function onUnitTestsList( array &$files ) {
+               // @codeCoverageIgnoreStart
+               $directoryIterator = new RecursiveDirectoryIterator( __DIR__ . 
'/tests/' );
+
+               /**
+                * @var SplFileInfo $fileInfo
+                */
+               $ourFiles = array();
+               foreach ( new RecursiveIteratorIterator( $directoryIterator ) 
as $fileInfo ) {
+                       if ( substr( $fileInfo->getFilename(), -8 ) === 
'Test.php' ) {
+                               $ourFiles[] = $fileInfo->getPathname();
+                       }
+               }
+
+               $files = array_merge( $files, $ourFiles );
+               return true;
+               // @codeCoverageIgnoreEnd
+       }
 }
diff --git a/googleAnalytics.php b/googleAnalytics.php
index 2085ae5..c43bc8e 100644
--- a/googleAnalytics.php
+++ b/googleAnalytics.php
@@ -42,3 +42,4 @@
 
 $wgAutoloadClasses['GoogleAnalyticsHooks'] = __DIR__ . 
'/googleAnalytics.hooks.php';
 $wgHooks['SkinAfterBottomScripts'][] = 
'GoogleAnalyticsHooks::onSkinAfterBottomScripts';
+$wgHooks['UnitTestsList'][] = 'GoogleAnalyticsHooks::onUnitTestsList';
diff --git a/tests/GoogleAnalyticsHooksTest.php 
b/tests/GoogleAnalyticsHooksTest.php
new file mode 100644
index 0000000..a3c1886
--- /dev/null
+++ b/tests/GoogleAnalyticsHooksTest.php
@@ -0,0 +1,96 @@
+<?php
+
+/**
+ * @covers GoogleAnalyticsHooks
+ */
+class GoogleAnalyticsHooksTest extends MediaWikiLangTestCase {
+       public function setUp() {
+               parent::setUp();
+               $this->setMwGlobals( 'wgGoogleAnalyticsAccount', '' );
+       }
+       /**
+        * @param $allowed
+        * @return Skin
+        */
+       private function mockSkin( $allowed, $title = 'Main Page' ) {
+               $skin = $this->getMockBuilder( 'SkinFallback' )
+                       ->disableOriginalConstructor()
+                       ->setMethods( array( 'getUser', 'getTitle' ) )
+                       ->getMock();
+               $user = $this->getMockBuilder( 'User' )
+                       ->disableOriginalConstructor()
+                       ->setMethods( array( 'isAllowed' ) )
+                       ->getMock();
+
+               $user->expects( $this->any() )
+                       ->method( 'isAllowed' )
+                       ->will( $this->returnValue( $allowed ) );
+               $skin
+                       ->expects( $this->any() )
+                       ->method( 'getUser' )
+                       ->will( $this->returnValue( $user ) );
+
+               $skin->expects( $this->any() )
+                       ->method( 'getTitle' )
+                       ->will( $this->returnValue( Title::newFromText( $title 
) ) );
+
+               return $skin;
+       }
+
+       /**
+        * @dataProvider provideUserPermissions
+        */
+       public function testUserPermissions( $allowed, $expected ) {
+               $text = '';
+               GoogleAnalyticsHooks::onSkinAfterBottomScripts( 
$this->mockSkin( $allowed ), $text );
+               $this->assertContains( $expected, $text );
+       }
+
+       public static function provideUserPermissions() {
+               return array(
+                       array( false, 'No web analytics configured' ),
+                       array( true, 'Web analytics code inclusion is disabled 
for this user' ),
+               );
+       }
+
+       public function testAccountIdSet() {
+               $this->setMwGlobals( 'wgGoogleAnalyticsAccount', 'foobarbaz' );
+               $text = '';
+               GoogleAnalyticsHooks::onSkinAfterBottomScripts( 
$this->mockSkin( false ), $text );
+               $this->assertContains( 'www.google-analytics.com/analytics.js', 
$text );
+               $this->assertContains( 'foobarbaz', $text );
+               $this->setMwGlobals( 'wgGoogleAnalyticsAccount', '' );
+               GoogleAnalyticsHooks::onSkinAfterBottomScripts( 
$this->mockSkin( false ), $text );
+               $this->assertContains( 'No web analytics configured', $text );
+       }
+
+       /**
+        * @dataProvider provideExcludedPages
+        */
+       public function testExcludedPages( $type, $conf, $title, $include ) {
+               $this->setMwGlobals( $type, array( $conf ) );
+               /*if ( $type == 'special' ) {
+                       $this->setMwGlobals( 'wgGoogleAnalyticsIgnoreSpecials', 
array( $title ) );
+               } elseif ( $type == 'nsid' ) {
+                       $this->setMwGlobals( '$wgGoogleAnalyticsIgnoreNsIDs', 
arr)
+               }*/
+               $text = '';
+               GoogleAnalyticsHooks::onSkinAfterBottomScripts( 
$this->mockSkin( false, $title ), $text );
+               if ( $include ) {
+                       $this->assertContains( 'No web analytics configured', 
$text );
+               } else {
+                       $this->assertContains( 'Web analytics code inclusion is 
disabled for this page', $text );
+               }
+       }
+
+       public static function provideExcludedPages() {
+               return array(
+                       array( 'wgGoogleAnalyticsIgnoreSpecials', 
'Preferences', 'Special:Preferences', false ),
+                       array( 'wgGoogleAnalyticsIgnoreSpecials', 'Userlogout', 
'Special:Preferences', true ),
+                       array( 'wgGoogleAnalyticsIgnoreNsIDs', NS_HELP, 
'Help:FooBar', false ),
+                       array( 'wgGoogleAnalyticsIgnoreNsIDs', NS_MAIN, 
'Help:FooBar', true ),
+                       array( 'wgGoogleAnalyticsIgnorePages', 'Help:FooBar', 
'Help:FooBar', false ),
+                       array( 'wgGoogleAnalyticsIgnorePages', 'Help:FooBar', 
'Help:FooBarBaz', true ),
+               );
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/185793
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7b8b3ce3f44e5366ed9ed28fff8e83a531cddd5e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/googleAnalytics
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to