From 64090974adf8343218d307436a1c6d6923bef3fb Mon Sep 17 00:00:00 2001 From: Georg Ehrke Date: Sun, 3 May 2015 19:27:45 +0200 Subject: [PATCH 1/3] mimic core's getEditionString() to comply with code guidelines --- lib/util.php | 38 ++++++++++++++++++++++++++++++++++++++ templates/wizard.php | 6 +++--- wizard.php | 1 + 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 lib/util.php diff --git a/lib/util.php b/lib/util.php new file mode 100644 index 000000000000..318033e21983 --- /dev/null +++ b/lib/util.php @@ -0,0 +1,38 @@ +. + * + */ + +namespace OCA\FirstRunWizard; + +class Util { + + /** + * mimic \OC_Util::getEditionString() + * @return string + */ + public static function getEdition() { + if (\OC::$server->getAppManager()->isEnabledForUser('enterprise_key')) { + return "Enterprise"; + } + return ''; + } +} \ No newline at end of file diff --git a/templates/wizard.php b/templates/wizard.php index df5122ef52fd..b5d51e2a9198 100644 --- a/templates/wizard.php +++ b/templates/wizard.php @@ -4,7 +4,7 @@

t('Welcome to %s', array($theme->getTitle()))); ?>

- +

t('Your personal web services. All your files, contacts, calendar and more, in one place.'));?>

getSlogan()); ?>

@@ -25,7 +25,7 @@ alt="t('iOS app'));?>" /> - +

t('Connect your desktop apps to %s', array($theme->getName()))); ?>

- + t('There’s more information in the documentation and on our website.', array(link_to_docs('user_manual')))); ?>
t('If you like ownCloud, . -* -*/ + * ownCloud - firstrunwizard application + * + * @author Frank Karlitschek + * @copyright 2012 Frank Karlitschek frank@owncloud.org + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see . + * + */ +namespace OCA\FirstRunWizard; -OCP\JSON::checkLoggedIn(); -OCP\JSON::checkAppEnabled('firstrunwizard'); -OCP\JSON::callCheck(); +use OCP\JSON; -\OCA_FirstRunWizard\Config::disable(); +JSON::checkLoggedIn(); +JSON::checkAppEnabled('firstrunwizard'); +JSON::callCheck(); +$config = \OC::$server->getConfig(); +$userSession = \OC::$server->getUserSession(); +$firstRunConfig = new Config($config, $userSession); +$firstRunConfig->disable(); diff --git a/ajax/enable.php b/ajax/enable.php index 8fb749b477ee..903627136f24 100644 --- a/ajax/enable.php +++ b/ajax/enable.php @@ -1,30 +1,34 @@ . -* -*/ + * ownCloud - firstrunwizard application + * + * @author Frank Karlitschek + * @copyright 2012 Frank Karlitschek frank@owncloud.org + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see . + * + */ +namespace OCA\FirstRunWizard; -OCP\JSON::checkLoggedIn(); -OCP\JSON::checkAppEnabled('firstrunwizard'); -OCP\JSON::callCheck(); +use OCP\JSON; -\OCA_FirstRunWizard\Config::enable(); +JSON::checkLoggedIn(); +JSON::checkAppEnabled('firstrunwizard'); +JSON::callCheck(); +$config = \OC::$server->getConfig(); +$userSession = \OC::$server->getUserSession(); +$firstRunConfig = new Config($config, $userSession); +$firstRunConfig->enable(); diff --git a/appinfo/app.php b/appinfo/app.php index cee38b27940c..8286f85619f2 100755 --- a/appinfo/app.php +++ b/appinfo/app.php @@ -1,5 +1,4 @@ . * */ +namespace OCA\FirstRunWizard; +use OCP\Util; -OC::$CLASSPATH['OCA_FirstRunWizard\Config'] = 'firstrunwizard/lib/firstrunwizard.php'; +Util::addStyle('firstrunwizard', 'colorbox'); +Util::addScript('firstrunwizard', 'jquery.colorbox'); +Util::addScript('firstrunwizard', 'firstrunwizard'); -OCP\Util::addStyle( 'firstrunwizard', 'colorbox'); -OCP\Util::addScript( 'firstrunwizard', 'jquery.colorbox'); -OCP\Util::addScript( 'firstrunwizard', 'firstrunwizard'); +Util::addStyle('firstrunwizard', 'firstrunwizard'); -OCP\Util::addStyle('firstrunwizard', 'firstrunwizard'); +$config = \OC::$server->getConfig(); +$userSession = \OC::$server->getUserSession(); +$firstRunConfig = new Config($config, $userSession); -if(\OCP\User::isLoggedIn() and \OCA_FirstRunWizard\Config::isEnabled()){ - OCP\Util::addScript( 'firstrunwizard', 'activate'); +if ($userSession->isLoggedIn() && $firstRunConfig->isEnabled()) { + Util::addScript( 'firstrunwizard', 'activate'); } diff --git a/lib/config.php b/lib/config.php new file mode 100755 index 000000000000..e04089104bfa --- /dev/null +++ b/lib/config.php @@ -0,0 +1,76 @@ +. + * + */ +namespace OCA\FirstRunWizard; + +use OCP\IConfig; +use OCP\IUserSession; + +class Config { + + protected $config; + + protected $userSession; + + /** + * @param IConfig $config + * @param IUserSession $userSession + */ + public function __construct(IConfig $config, IUserSession $userSession) { + $this->config = $config; + $this->userSession = $userSession; + } + + /** + * @brief Disable the FirstRunWizard + */ + public function enable() { + $user = $this->userSession->getUser(); + if ($user) { + $this->config->setUserValue($user->getUID(), 'firstrunwizard', 'show', 1); + } + } + + /** + * @brief Enable the FirstRunWizard + */ + public function disable() { + $user = $this->userSession->getUser(); + if ($user) { + $this->config->setUserValue($user->getUID(), 'firstrunwizard', 'show', 0); + } + } + + /** + * @brief Check if the FirstRunWizard is enabled or not + * @return bool + */ + public function isEnabled() { + $user = $this->userSession->getUser(); + if ($user) { + $conf = $this->config->getUserValue($user->getUID(), 'firstrunwizard', 'show', 1); + return (intval($conf) === 1); + } else { + return false; + } + } +} diff --git a/lib/firstrunwizard.php b/lib/firstrunwizard.php deleted file mode 100755 index 58f0b8ec63c0..000000000000 --- a/lib/firstrunwizard.php +++ /dev/null @@ -1,54 +0,0 @@ -. - * - */ - - -namespace OCA_FirstRunWizard; - -class Config { - - /** - * @brief Disable the FirstRunWizard - */ - public static function enable() { - \OCP\Config::setUserValue( \OCP\User::getUser(), 'firstrunwizard', 'show', 1 ); - } - - /** - * @brief Enable the FirstRunWizard - */ - public static function disable() { - \OCP\Config::setUserValue( \OCP\User::getUser(), 'firstrunwizard', 'show', 0 ); - } - - /** - * @brief Check if the FirstRunWizard is enabled or not - * @return bool - */ - public static function isEnabled() { - $conf = \OCP\CONFIG::getUserValue( \OCP\User::getUser() , 'firstrunwizard' , 'show' , 1 ); - return (intval($conf) === 1); - } - - - -} diff --git a/lib/util.php b/lib/util.php index 318033e21983..5f10c0ee569e 100644 --- a/lib/util.php +++ b/lib/util.php @@ -20,19 +20,50 @@ * License along with this library. If not, see . * */ - namespace OCA\FirstRunWizard; +use OCP\App\IAppManager; +use OCP\Defaults; +use OCP\IConfig; + class Util { + protected $appManager; + + protected $config; + + protected $defaults; + + /** + * @param IAppManager $appManager + * @param IConfig $config + * @param Defaults $defaults + */ + public function __construct(IAppManager $appManager, IConfig $config, Defaults $defaults) { + $this->appManager = $appManager; + $this->config = $config; + $this->defaults = $defaults; + } + /** * mimic \OC_Util::getEditionString() * @return string */ - public static function getEdition() { - if (\OC::$server->getAppManager()->isEnabledForUser('enterprise_key')) { - return "Enterprise"; + public function getEdition() { + if ($this->appManager->isEnabledForUser('enterprise_key')) { + return 'Enterprise'; } return ''; } + + /** + * @return array + */ + public function getSyncClientUrls() { + return array( + 'desktop' => $this->config->getSystemValue('customclient_desktop', $this->defaults->getSyncClientUrl()), + 'android' => $this->config->getSystemValue('customclient_android', $this->defaults->getAndroidClientUrl()), + 'ios' => $this->config->getSystemValue('customclient_ios', $this->defaults->getiOSClientUrl()) + ); + } } \ No newline at end of file diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000000..b7f9915b055f --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,14 @@ +getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $userSession = $this->getMockBuilder('\OCP\IUserSession') + ->disableOriginalConstructor()->getMock(); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + + if ($isUserAvailable) { + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $config->expects($this->once()) + ->method('setUserValue') + ->with('user123', 'firstrunwizard', 'show', 1); + + $c = new Config($config, $userSession); + $c->enable(); + } else { + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue(null)); + $user->expects($this->never()) + ->method('getUID'); + $config->expects($this->never()) + ->method('setUserValue'); + + $c = new Config($config, $userSession); + $c->enable(); + } + } + + /** + * @dataProvider enableDisableData + */ + public function testDisable($isUserAvailable) { + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $userSession = $this->getMockBuilder('\OCP\IUserSession') + ->disableOriginalConstructor()->getMock(); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + + if ($isUserAvailable) { + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $config->expects($this->once()) + ->method('setUserValue') + ->with('user123', 'firstrunwizard', 'show', 0); + + $c = new Config($config, $userSession); + $c->disable(); + } else { + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue(null)); + $user->expects($this->never()) + ->method('getUID'); + $config->expects($this->never()) + ->method('setUserValue'); + + $c = new Config($config, $userSession); + $c->disable(); + } + } + + public function enableDisableData() { + return [ + [true], + [false], + ]; + } + + /** + * @dataProvider isEnabledData + */ + public function testIsEnabled($isEnabled) { + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $userSession = $this->getMockBuilder('\OCP\IUserSession') + ->disableOriginalConstructor()->getMock(); + $user = $this->getMockBuilder('\OCP\IUser') + ->disableOriginalConstructor()->getMock(); + + if ($isEnabled === true) { + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $config->expects($this->once()) + ->method('getUserValue') + ->with('user123', 'firstrunwizard', 'show', 1) + ->will($this->returnValue(1)); + + $c = new Config($config, $userSession); + $this->assertEquals(true, $c->isEnabled()); + } elseif ($isEnabled === false) { + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + + $config->expects($this->once()) + ->method('getUserValue') + ->with('user123', 'firstrunwizard', 'show', 1) + ->will($this->returnValue(0)); + + $c = new Config($config, $userSession); + $this->assertEquals(false, $c->isEnabled()); + } elseif ($isEnabled === null) { + $userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue(null)); + $user->expects($this->never()) + ->method('getUID'); + $config->expects($this->never()) + ->method('getUserValue'); + + $c = new Config($config, $userSession); + $this->assertEquals(false, $c->isEnabled()); + } + } + + public function isEnabledData() { + return [ + [true], + [false], + [null], + ]; + } +} \ No newline at end of file diff --git a/tests/lib/utiltest.php b/tests/lib/utiltest.php new file mode 100644 index 000000000000..631855a354bd --- /dev/null +++ b/tests/lib/utiltest.php @@ -0,0 +1,86 @@ +getMockBuilder('\OCP\App\IAppManager') + ->disableOriginalConstructor()->getMock(); + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $defaults = $this->getMockBuilder('\OCP\Defaults') + ->disableOriginalConstructor()->getMock(); + + $appManager->expects($this->once()) + ->method('isEnabledForUser') + ->with('enterprise_key') + ->will($this->returnValue($isEnterpriseKeyEnabled)); + + $util = new Util($appManager, $config, $defaults); + $this->assertEquals($string, $util->getEdition()); + } + + public function getEditionData() { + return [ + [true, 'Enterprise'], + [false, ''], + ]; + } + + /** + * @dataProvider getSyncClientUrlsData + */ + public function testGetSyncClientUrls($configValues, $defaultValues) { + $appManager = $this->getMockBuilder('\OCP\App\IAppManager') + ->disableOriginalConstructor()->getMock(); + $config = $this->getMockBuilder('\OCP\IConfig') + ->disableOriginalConstructor()->getMock(); + $defaults = $this->getMockBuilder('\OCP\Defaults') + ->disableOriginalConstructor()->getMock(); + + $defaults->expects($this->once()) + ->method('getSyncClientUrl') + ->will($this->returnValue($defaultValues[0])); + $defaults->expects($this->once()) + ->method('getAndroidClientUrl') + ->will($this->returnValue($defaultValues[1])); + $defaults->expects($this->once()) + ->method('getiOSClientUrl') + ->will($this->returnValue($defaultValues[2])); + + $config->expects($this->at(0)) + ->method('getSystemValue') + ->with('customclient_desktop', $defaultValues[0]) + ->will($this->returnValue($configValues[0])); + $config->expects($this->at(1)) + ->method('getSystemValue') + ->with('customclient_android', $defaultValues[1]) + ->will($this->returnValue($configValues[1])); + $config->expects($this->at(2)) + ->method('getSystemValue') + ->with('customclient_ios', $defaultValues[2]) + ->will($this->returnValue($configValues[2])); + + $util = new Util($appManager, $config, $defaults); + $this->assertEquals([ + 'desktop' => $configValues[0], + 'android' => $configValues[1], + 'ios' => $configValues[2], + ], $util->getSyncClientUrls()); + } + + public function getSyncClientUrlsData() { + return [ + [ + ['a', 'b', 'c'], + ['d', 'e', 'f'], + ], + ]; + } +} \ No newline at end of file diff --git a/tests/phpunit.xml b/tests/phpunit.xml new file mode 100644 index 000000000000..5a11167a0461 --- /dev/null +++ b/tests/phpunit.xml @@ -0,0 +1,25 @@ + + + + . + + + + + ../firstrunwizard + + ../firstrunwizard/l10n + ../firstrunwizard/tests + + + + + + + + \ No newline at end of file diff --git a/wizard.php b/wizard.php index 756265ea8337..e412371c0276 100644 --- a/wizard.php +++ b/wizard.php @@ -20,23 +20,24 @@ * License along with this library. If not, see . * */ +namespace OCA\FirstRunWizard; +use OCP\Defaults; +use OCP\User; +use OCP\Util as CoreUtil; +use OCP\Template; // Check if we are a user -OCP\User::checkLoggedIn(); +User::checkLoggedIn(); -$defaults = new \OCP\Defaults(); +$appManager = \OC::$server->getAppManager(); +$config = \OC::$server->getConfig(); +$defaults = new Defaults(); -//links to clients -$clients = array( - 'desktop' => OCP\Config::getSystemValue('customclient_desktop', $defaults->getSyncClientUrl()), - 'android' => OCP\Config::getSystemValue('customclient_android', $defaults->getAndroidClientUrl()), - 'ios' => OCP\Config::getSystemValue('customclient_ios', $defaults->getiOSClientUrl()) -); +$util = new Util($appManager, $config, $defaults); -$tmpl = new OCP\Template( 'firstrunwizard', 'wizard', '' ); -$tmpl->assign('logo', OCP\Util::linkTo('core','img/logo-inverted.svg')); -$tmpl->assign('clients', $clients); -$tmpl->assign('edition', OCA\FirstRunWizard\Util::getEdition()); +$tmpl = new Template('firstrunwizard', 'wizard', ''); +$tmpl->assign('logo', CoreUtil::linkTo('core','img/logo-inverted.svg')); +$tmpl->assign('clients', $util->getSyncClientUrls()); +$tmpl->assign('edition', $util->getEdition()); $tmpl->printPage(); -