Ljonka has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/357352 )

Change subject: Add PHPUnit Tests, uploadfile is ignored in this version
......................................................................

Add PHPUnit Tests, uploadfile is ignored in this version

Change-Id: Ie51768a0129b9001744176ef48cd0447108b8445
---
M Avatars/Avatars.class.php
M Avatars/extension.json
A Avatars/tests/phpunit/BSApiAvatarsTasksTest.php
3 files changed, 363 insertions(+), 1 deletion(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceExtensions 
refs/changes/52/357352/1

diff --git a/Avatars/Avatars.class.php b/Avatars/Avatars.class.php
index 86e9604..b0d9a1a 100644
--- a/Avatars/Avatars.class.php
+++ b/Avatars/Avatars.class.php
@@ -318,4 +318,16 @@
                }
                return true;
        }
+
+       /**
+        * UnitTestsList allows registration of additional test suites to 
execute
+        * under PHPUnit. Extensions can append paths to files to the $paths 
array,
+        * and since MediaWiki 1.24, can specify paths to directories, which 
will
+        * be scanned recursively for any test case files with the suffix 
"Test.php".
+        * @param array $paths
+        */
+       public static function onUnitTestsList( array &$paths ) {
+               $paths[] = __DIR__ . '/tests/phpunit/';
+               return true;
+       }
 }
diff --git a/Avatars/extension.json b/Avatars/extension.json
index 806b80d..3a246fa 100644
--- a/Avatars/extension.json
+++ b/Avatars/extension.json
@@ -52,7 +52,8 @@
        "Hooks": {
                "BeforePageDisplay": "Avatars::onBeforePageDisplay",
                "BSUserManagerAfterAddUser": 
"Avatars::onBSUserManagerAfterAddUser",
-               "LocalUserCreated": "Avatars::onLocalUserCreated"
+               "LocalUserCreated": "Avatars::onLocalUserCreated",
+               "UnitTestsList": "Avatars::onUnitTestsList"
        },
        "manifest_version": 1
 }
diff --git a/Avatars/tests/phpunit/BSApiAvatarsTasksTest.php 
b/Avatars/tests/phpunit/BSApiAvatarsTasksTest.php
new file mode 100644
index 0000000..fbce5c2
--- /dev/null
+++ b/Avatars/tests/phpunit/BSApiAvatarsTasksTest.php
@@ -0,0 +1,349 @@
+wi<?php
+
+/*
+ * Test BlueSpiceAvatars API Endpoints
+ */
+
+/**
+ * @group BlueSpiceAvatars
+ * @group BlueSpice
+ * @group API
+ * @group Database
+ * @group medium
+ */
+class BSApiAvatarsTasksTest extends BSApiTasksTestBase {
+
+       protected function getModuleName() {
+               return "bs-avatars-tasks";
+       }
+
+       /**
+        * 'generateAvatar' => [
+         'examples' => [],
+         'params' => []
+         ],
+        * @return type
+        */
+       function testGenerateAvatar() {
+               $data = $this->executeTask(
+                 'generateAvatar', []
+               );
+
+               $this->assertEquals( true, $data->success );
+
+       }
+
+       /**
+        * 'setUserImage' => [
+         'examples' => [
+         [
+         'userImage' => 'ProfileImage.png'
+         ]
+         ],
+         'params' => [
+         'userImage' => [
+         'desc' => 'Name of the image to set',
+         'type' => 'string',
+         'required' => true
+         ]
+         ]
+         ]
+        * @return type
+        */
+       function testSetUserImage( ) {
+               $user = self::$users['uploader'];
+
+               $params = [
+                       'action' => 'login',
+                       'lgname' => $user->username,
+                       'lgpassword' => $user->password
+               ];
+               list( $result, ,$session ) = $this->doApiRequest( $params );
+               $this->assertArrayHasKey( "login", $result );
+               $this->assertArrayHasKey( "result", $result['login'] );
+               $this->assertEquals( "NeedToken", $result['login']['result'] );
+               $token = $result['login']['token'];
+
+               $params = [
+                       'action' => 'login',
+                       'lgtoken' => $token,
+                       'lgname' => $user->username,
+                       'lgpassword' => $user->password
+               ];
+               list( $result, , $session ) = $this->doApiRequest( $params, 
$session );
+               $this->assertArrayHasKey( "login", $result );
+               $this->assertArrayHasKey( "result", $result['login'] );
+               $this->assertEquals( "Success", $result['login']['result'] );
+               $this->assertArrayHasKey( 'lgtoken', $result['login'] );
+
+               $this->assertNotEmpty( $session, 'API Login must return a 
session' );
+
+               //create example image
+               $extension = 'png';
+               $mimeType = 'image/png';
+
+               try {
+                       $randomImageGenerator = new RandomImageGenerator();
+                       $filePaths = $randomImageGenerator->writeImages( 1, 
$extension, $this->getNewTempDirectory() );
+               } catch ( Exception $e ) {
+                       $this->markTestIncomplete( $e->getMessage() );
+               }
+
+               /** @var array $filePaths */
+               $filePath = $filePaths[0];
+               $fileSize = filesize( $filePath );
+               $fileName = basename( $filePath );
+
+               $this->deleteFileByFileName( $fileName );
+               $this->deleteFileByContent( $filePath );
+
+               if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, 
$filePath ) ) {
+                       $this->markTestIncomplete( "Couldn't upload file!\n" );
+               }
+
+               $params = [
+                       'action' => 'upload',
+                       'filename' => $fileName,
+                       'file' => 'dummy content',
+                       'comment' => 'dummy comment',
+                       'text' => "This is the page text for $fileName",
+               ];
+
+               $exception = false;
+               try {
+                       list( $result, , ) = $this->doApiRequestWithToken( 
$params, $session,
+                               self::$users['uploader']->getUser() );
+               } catch ( UsageException $e ) {
+                       $exception = true;
+               }
+               $this->assertTrue( isset( $result['upload'] ) );
+               $this->assertEquals( 'Success', $result['upload']['result'] );
+               $this->assertEquals( $fileSize, 
(int)$result['upload']['imageinfo']['size'] );
+               $this->assertEquals( $mimeType, 
$result['upload']['imageinfo']['mime'] );
+               $this->assertFalse( $exception );
+
+               //Set example image as userimage
+
+               $data = $this->executeTask(
+                 'setUserImage', [
+                               "userImage" => "File:" . $fileName
+                 ]
+               );
+
+               $this->assertEquals( true, $data->success, "Couldn't set user 
imag!\n" );
+
+               // clean up
+               $this->deleteFileByFileName( $fileName );
+
+               return $data;
+       }
+
+       /**
+        *
+        * 'uploadFile' => [
+         'examples' => [],
+         'params' => []
+         ],
+        *
+        * @return type
+        */
+       function testUploadFile() {
+               $user = self::$users['uploader'];
+
+               $params = [
+                       'action' => 'login',
+                       'lgname' => $user->username,
+                       'lgpassword' => $user->password
+               ];
+               list( $result, ,$session ) = $this->doApiRequest( $params );
+               $this->assertArrayHasKey( "login", $result );
+               $this->assertArrayHasKey( "result", $result['login'] );
+               $this->assertEquals( "NeedToken", $result['login']['result'] );
+               $token = $result['login']['token'];
+
+               $params = [
+                       'action' => 'login',
+                       'lgtoken' => $token,
+                       'lgname' => $user->username,
+                       'lgpassword' => $user->password
+               ];
+               list( $result, , $session ) = $this->doApiRequest( $params, 
$session );
+               $this->assertArrayHasKey( "login", $result );
+               $this->assertArrayHasKey( "result", $result['login'] );
+               $this->assertEquals( "Success", $result['login']['result'] );
+               $this->assertArrayHasKey( 'lgtoken', $result['login'] );
+
+               $this->assertNotEmpty( $session, 'API Login must return a 
session' );
+
+               //create example image
+               $extension = 'png';
+               $mimeType = 'image/png';
+
+               try {
+                       $randomImageGenerator = new RandomImageGenerator();
+                       $filePaths = $randomImageGenerator->writeImages( 1, 
$extension, $this->getNewTempDirectory() );
+               } catch ( Exception $e ) {
+                       $this->markTestIncomplete( $e->getMessage() );
+               }
+
+               /** @var array $filePaths */
+               $filePath = $filePaths[0];
+               $fileSize = filesize( $filePath );
+               $fileName = basename( $filePath );
+
+
+               if ( !$this->fakeUploadFile( 'file', $fileName, $mimeType, 
$filePath ) ) {
+                       $this->markTestIncomplete( "Couldn't upload file!\n" );
+               }
+
+               //TODO: Complete test!
+               $this->markTestIncomplete(
+          'This test has not been implemented yet.'
+        );
+               $_GET["name"] = $fileName;
+
+               $data = $this->executeTask(
+                 'uploadFile', []
+               );
+
+               $this->assertEquals( true, $data->success, $data->message);
+
+
+               $this->deleteFileByFileName( $fileName );
+               $this->deleteFileByContent( $filePath );
+       }
+
+       /**
+        * Helper function -- remove files and associated articles by Title
+        *
+        * @param Title $title Title to be removed
+        *
+        * @return bool
+        */
+       public function deleteFileByTitle( $title ) {
+               if ( $title->exists() ) {
+                       $file = wfFindFile( $title, [ 'ignoreRedirect' => true 
] );
+                       $noOldArchive = ""; // yes this really needs to be set 
this way
+                       $comment = "removing for test";
+                       $restrictDeletedVersions = false;
+                       $status = FileDeleteForm::doDelete(
+                               $title,
+                               $file,
+                               $noOldArchive,
+                               $comment,
+                               $restrictDeletedVersions
+                       );
+
+                       if ( !$status->isGood() ) {
+                               return false;
+                       }
+
+                       $page = WikiPage::factory( $title );
+                       $page->doDeleteArticle( "removing for test" );
+
+                       // see if it now doesn't exist; reload
+                       $title = Title::newFromText( $title->getText(), NS_FILE 
);
+               }
+
+               return !( $title && $title instanceof Title && $title->exists() 
);
+       }
+
+       /**
+        * Helper function -- remove files and associated articles with a 
particular filename
+        *
+        * @param string $fileName Filename to be removed
+        *
+        * @return bool
+        */
+       public function deleteFileByFileName( $fileName ) {
+               return $this->deleteFileByTitle( Title::newFromText( $fileName, 
NS_FILE ) );
+       }
+
+       /**
+        * Helper function -- given a file on the filesystem, find matching
+        * content in the db (and associated articles) and remove them.
+        *
+        * @param string $filePath Path to file on the filesystem
+        *
+        * @return bool
+        */
+       public function deleteFileByContent( $filePath ) {
+               $hash = FSFile::getSha1Base36FromPath( $filePath );
+               $dupes = RepoGroup::singleton()->findBySha1( $hash );
+               $success = true;
+               foreach ( $dupes as $dupe ) {
+                       $success &= $this->deleteFileByTitle( $dupe->getTitle() 
);
+               }
+
+               return $success;
+       }
+
+       /**
+        * Fake an upload by dumping the file into temp space, and adding info 
to $_FILES.
+        * (This is what PHP would normally do).
+        *
+        * @param string $fieldName Name this would have in the upload form
+        * @param string $fileName Name to title this
+        * @param string $type MIME type
+        * @param string $filePath Path where to find file contents
+        *
+        * @throws Exception
+        * @return bool
+        */
+       function fakeUploadFile( $fieldName, $fileName, $type, $filePath ) {
+               $tmpName = $this->getNewTempFile();
+               if ( !file_exists( $filePath ) ) {
+                       throw new Exception( "$filePath doesn't exist!" );
+               }
+
+               if ( !copy( $filePath, $tmpName ) ) {
+                       throw new Exception( "couldn't copy $filePath to 
$tmpName" );
+               }
+
+               clearstatcache();
+               $size = filesize( $tmpName );
+               if ( $size === false ) {
+                       throw new Exception( "couldn't stat $tmpName" );
+               }
+
+               $_FILES[$fieldName] = [
+                       'name' => $fileName,
+                       'type' => $type,
+                       'tmp_name' => $tmpName,
+                       'size' => $size,
+                       'error' => null
+               ];
+
+               return true;
+       }
+
+       function fakeUploadChunk( $fieldName, $fileName, $type, & $chunkData ) {
+               $tmpName = $this->getNewTempFile();
+               // copy the chunk data to temp location:
+               if ( !file_put_contents( $tmpName, $chunkData ) ) {
+                       throw new Exception( "couldn't copy chunk data to 
$tmpName" );
+               }
+
+               clearstatcache();
+               $size = filesize( $tmpName );
+               if ( $size === false ) {
+                       throw new Exception( "couldn't stat $tmpName" );
+               }
+
+               $_FILES[$fieldName] = [
+                       'name' => $fileName,
+                       'type' => $type,
+                       'tmp_name' => $tmpName,
+                       'size' => $size,
+                       'error' => null
+               ];
+       }
+
+       /**
+        * Remove traces of previous fake uploads
+        */
+       function clearFakeUploads() {
+               $_FILES = [];
+       }
+
+}

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie51768a0129b9001744176ef48cd0447108b8445
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceExtensions
Gerrit-Branch: master
Gerrit-Owner: Ljonka <[email protected]>

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

Reply via email to