]> git.mxchange.org Git - friendica.git/blobdiff - tests/src/Model/User/CookieTest.php
Fix wrong `$this->assert...()` with `self::assert...()
[friendica.git] / tests / src / Model / User / CookieTest.php
index bff1698e8375d7215199114a5c16f43cd5007707..e8dba95576ae4d369edee29a1018038413cf81f0 100644 (file)
@@ -1,17 +1,39 @@
 <?php
-
-namespace Friendica\Testsrc\Model\User;
-
-use Friendica\Core\Config\Configuration;
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program 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 program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Test\src\Model\User;
+
+use Friendica\App\BaseURL;
+use Friendica\Core\Config\IConfig;
 use Friendica\Model\User\Cookie;
-use Friendica\Test\DatabaseTest;
+use Friendica\Test\MockedTest;
 use Friendica\Test\Util\StaticCookie;
 use Mockery\MockInterface;
 
-class CookieTest extends DatabaseTest
+class CookieTest extends MockedTest
 {
-       /** @var MockInterface|Configuration */
+       /** @var MockInterface|IConfig */
        private $config;
+       /** @var MockInterface|BaseURL */
+       private $baseUrl;
 
        protected function setUp()
        {
@@ -19,7 +41,8 @@ class CookieTest extends DatabaseTest
 
                parent::setUp();
 
-               $this->config = \Mockery::mock(Configuration::class);
+               $this->config = \Mockery::mock(IConfig::class);
+               $this->baseUrl = \Mockery::mock(BaseURL::class);
        }
 
        protected function tearDown()
@@ -32,12 +55,12 @@ class CookieTest extends DatabaseTest
         */
        public function testInstance()
        {
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
 
-               $cookie = new Cookie($this->config, []);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new Cookie($this->config, $this->baseUrl);
+               self::assertInstanceOf(Cookie::class, $cookie);
        }
 
        public function dataGet()
@@ -96,36 +119,36 @@ class CookieTest extends DatabaseTest
         */
        public function testGet(array $cookieData, bool $hasValues, $uid, $hash, $ip)
        {
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn('1235')->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
 
-               $cookie = new Cookie($this->config, [], $cookieData);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new Cookie($this->config, $this->baseUrl, [], $cookieData);
+               self::assertInstanceOf(Cookie::class, $cookie);
 
                $assertData = $cookie->getData();
 
                if (!$hasValues) {
-                       $this->assertEmpty($assertData);
+                       self::assertEmpty($assertData);
                } else {
-                       $this->assertNotEmpty($assertData);
+                       self::assertNotEmpty($assertData);
                        if (isset($uid)) {
-                               $this->assertObjectHasAttribute('uid', $assertData);
-                               $this->assertEquals($uid, $assertData->uid);
+                               self::assertObjectHasAttribute('uid', $assertData);
+                               self::assertEquals($uid, $assertData->uid);
                        } else {
-                               $this->assertObjectNotHasAttribute('uid', $assertData);
+                               self::assertObjectNotHasAttribute('uid', $assertData);
                        }
                        if (isset($hash)) {
-                               $this->assertObjectHasAttribute('hash', $assertData);
-                               $this->assertEquals($hash, $assertData->hash);
+                               self::assertObjectHasAttribute('hash', $assertData);
+                               self::assertEquals($hash, $assertData->hash);
                        } else {
-                               $this->assertObjectNotHasAttribute('hash', $assertData);
+                               self::assertObjectNotHasAttribute('hash', $assertData);
                        }
                        if (isset($ip)) {
-                               $this->assertObjectHasAttribute('ip', $assertData);
-                               $this->assertEquals($ip, $assertData->ip);
+                               self::assertObjectHasAttribute('ip', $assertData);
+                               self::assertEquals($ip, $assertData->ip);
                        } else {
-                               $this->assertObjectNotHasAttribute('ip', $assertData);
+                               self::assertObjectNotHasAttribute('ip', $assertData);
                        }
                }
        }
@@ -164,14 +187,14 @@ class CookieTest extends DatabaseTest
         */
        public function testCheck(string $serverPrivateKey, string $userPrivateKey, string $password, string $assertHash, bool $assertTrue)
        {
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverPrivateKey)->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn('7')->once();
 
-               $cookie = new Cookie($this->config, []);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new Cookie($this->config, $this->baseUrl);
+               self::assertInstanceOf(Cookie::class, $cookie);
 
-               $this->assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
+               self::assertEquals($assertTrue, $cookie->check($assertHash, $password, $userPrivateKey));
        }
 
        public function dataSet()
@@ -222,21 +245,21 @@ class CookieTest extends DatabaseTest
 
        public function assertCookie($uid, $hash, $remoteIp, $lifetime)
        {
-               $this->assertArrayHasKey(Cookie::NAME, StaticCookie::$_COOKIE);
+               self::assertArrayHasKey(Cookie::NAME, StaticCookie::$_COOKIE);
 
                $data = json_decode(StaticCookie::$_COOKIE[Cookie::NAME]);
 
-               $this->assertObjectHasAttribute('uid', $data);
-               $this->assertEquals($uid, $data->uid);
-               $this->assertObjectHasAttribute('hash', $data);
-               $this->assertEquals($hash, $data->hash);
-               $this->assertObjectHasAttribute('ip', $data);
-               $this->assertEquals($remoteIp, $data->ip);
+               self::assertObjectHasAttribute('uid', $data);
+               self::assertEquals($uid, $data->uid);
+               self::assertObjectHasAttribute('hash', $data);
+               self::assertEquals($hash, $data->hash);
+               self::assertObjectHasAttribute('ip', $data);
+               self::assertEquals($remoteIp, $data->ip);
 
                if (isset($lifetime) && $lifetime !== 0) {
-                       $this->assertLessThanOrEqual(time() + $lifetime, StaticCookie::$_EXPIRE);
+                       self::assertLessThanOrEqual(time() + $lifetime, StaticCookie::$_EXPIRE);
                } else {
-                       $this->assertLessThanOrEqual(time() + Cookie::DEFAULT_EXPIRE * 24 * 60 * 60, StaticCookie::$_EXPIRE);
+                       self::assertLessThanOrEqual(time() + Cookie::DEFAULT_EXPIRE * 24 * 60 * 60, StaticCookie::$_EXPIRE);
                }
        }
 
@@ -247,16 +270,16 @@ class CookieTest extends DatabaseTest
         */
        public function testSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
        {
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
 
-               $cookie = new StaticCookie($this->config, $serverArray);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
+               self::assertInstanceOf(Cookie::class, $cookie);
 
                $cookie->set($uid, $password, $privateKey, $lifetime);
 
-               $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
+               self::assertCookie($uid, $assertHash, $remoteIp, $lifetime);
        }
 
        /**
@@ -266,19 +289,19 @@ class CookieTest extends DatabaseTest
         */
        public function testDoubleSet($serverKey, $uid, $password, $privateKey, $assertHash, $remoteIp, $serverArray, $lifetime)
        {
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn($serverKey)->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
 
-               $cookie = new StaticCookie($this->config, $serverArray);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new StaticCookie($this->config, $this->baseUrl, $serverArray);
+               self::assertInstanceOf(Cookie::class, $cookie);
 
                // Invalid set, should get overwritten
                $cookie->set(-1, 'invalid', 'nothing', -234);
 
                $cookie->set($uid, $password, $privateKey, $lifetime);
 
-               $this->assertCookie($uid, $assertHash, $remoteIp, $lifetime);
+               self::assertCookie($uid, $assertHash, $remoteIp, $lifetime);
        }
 
        /**
@@ -290,19 +313,19 @@ class CookieTest extends DatabaseTest
                        Cookie::NAME => 'test'
                ];
 
-               $this->config->shouldReceive('get')->with('system', 'ssl_policy')->andReturn(1)->once();
+               $this->baseUrl->shouldReceive('getSSLPolicy')->andReturn(true)->once();
                $this->config->shouldReceive('get')->with('system', 'site_prvkey')->andReturn(24)->once();
                $this->config->shouldReceive('get')->with('system', 'auth_cookie_lifetime', Cookie::DEFAULT_EXPIRE)->andReturn(Cookie::DEFAULT_EXPIRE)->once();
 
-               $cookie = new StaticCookie($this->config, []);
-               $this->assertInstanceOf(Cookie::class, $cookie);
+               $cookie = new StaticCookie($this->config, $this->baseUrl);
+               self::assertInstanceOf(Cookie::class, $cookie);
 
-               $this->assertEquals('test', StaticCookie::$_COOKIE[Cookie::NAME]);
-               $this->assertEquals(null, StaticCookie::$_EXPIRE);
+               self::assertEquals('test', StaticCookie::$_COOKIE[Cookie::NAME]);
+               self::assertEquals(null, StaticCookie::$_EXPIRE);
 
                $cookie->clear();
 
-               $this->assertEmpty(StaticCookie::$_COOKIE[Cookie::NAME]);
-               $this->assertEquals(-3600, StaticCookie::$_EXPIRE);
+               self::assertEmpty(StaticCookie::$_COOKIE[Cookie::NAME]);
+               self::assertEquals(-3600, StaticCookie::$_EXPIRE);
        }
 }