]> git.immae.eu Git - github/shaarli/Shaarli.git/commitdiff
Merge pull request #1575 from ArthurHoaro/feature/php8
authorArthurHoaro <arthur@hoa.ro>
Sat, 3 Oct 2020 10:59:16 +0000 (12:59 +0200)
committerGitHub <noreply@github.com>
Sat, 3 Oct 2020 10:59:16 +0000 (12:59 +0200)
1  2 
tests/api/ApiMiddlewareTest.php
tests/bookmark/LinkUtilsTest.php
tests/front/controller/admin/ManageShaareControllerTest/SaveBookmarkTest.php

index 32031750b44a4b520362bdd760340807f61930f9,7386e435c2a83f4ab438b4389bbe5202195309e0..86700840b35dfc03ef75e052e0365b4e8ee89e29
@@@ -18,7 -18,7 +18,7 @@@ use Slim\Http\Response
   *
   * @package Api
   */
- class ApiMiddlewareTest extends \PHPUnit\Framework\TestCase
+ class ApiMiddlewareTest extends \Shaarli\TestCase
  {
      /**
       * @var string datastore to test write operations
@@@ -26,7 -26,7 +26,7 @@@
      protected static $testDatastore = 'sandbox/datastore.php';
  
      /**
-      * @var \ConfigManager instance
+      * @var ConfigManager instance
       */
      protected $conf;
  
          @unlink(self::$testDatastore);
      }
  
 +    /**
 +     * Invoke the middleware with a valid token
 +     */
 +    public function testInvokeMiddlewareWithValidToken(): void
 +    {
 +        $next = function (Request $request, Response $response): Response {
 +            return $response;
 +        };
 +        $mw = new ApiMiddleware($this->container);
 +        $env = Environment::mock([
 +            'REQUEST_METHOD' => 'GET',
 +            'REQUEST_URI' => '/echo',
 +            'HTTP_AUTHORIZATION'=> 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard'),
 +        ]);
 +        $request = Request::createFromEnvironment($env);
 +        $response = new Response();
 +        /** @var Response $response */
 +        $response = $mw($request, $response, $next);
 +
 +        $this->assertEquals(200, $response->getStatusCode());
 +    }
 +
 +    /**
 +     * Invoke the middleware with a valid token
 +     * Using specific Apache CGI redirected authorization.
 +     */
 +    public function testInvokeMiddlewareWithValidTokenFromRedirectedHeader(): void
 +    {
 +        $next = function (Request $request, Response $response): Response {
 +            return $response;
 +        };
 +
 +        $token = 'Bearer ' . ApiUtilsTest::generateValidJwtToken('NapoleonWasALizard');
 +        $this->container->environment['REDIRECT_HTTP_AUTHORIZATION'] = $token;
 +        $mw = new ApiMiddleware($this->container);
 +        $env = Environment::mock([
 +            'REQUEST_METHOD' => 'GET',
 +            'REQUEST_URI' => '/echo',
 +        ]);
 +        $request = Request::createFromEnvironment($env);
 +        $response = new Response();
 +        /** @var Response $response */
 +        $response = $mw($request, $response, $next);
 +
 +        $this->assertEquals(200, $response->getStatusCode());
 +    }
 +
      /**
       * Invoke the middleware with the API disabled:
       * should return a 401 error Unauthorized.
          $this->assertEquals(401, $response->getStatusCode());
          $body = json_decode((string) $response->getBody());
          $this->assertEquals('Not authorized: API is disabled', $body->message);
-         $this->assertContains('ApiAuthorizationException', $body->stacktrace);
+         $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
      }
  
      /**
          $this->assertEquals(401, $response->getStatusCode());
          $body = json_decode((string) $response->getBody());
          $this->assertEquals('Not authorized: JWT token not provided', $body->message);
-         $this->assertContains('ApiAuthorizationException', $body->stacktrace);
+         $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
      }
  
      /**
          $this->assertEquals(401, $response->getStatusCode());
          $body = json_decode((string) $response->getBody());
          $this->assertEquals('Not authorized: Token secret must be set in Shaarli\'s administration', $body->message);
-         $this->assertContains('ApiAuthorizationException', $body->stacktrace);
+         $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
      }
  
      /**
          $this->assertEquals(401, $response->getStatusCode());
          $body = json_decode((string) $response->getBody());
          $this->assertEquals('Not authorized: Invalid JWT header', $body->message);
-         $this->assertContains('ApiAuthorizationException', $body->stacktrace);
+         $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
      }
  
      /**
          $this->assertEquals(401, $response->getStatusCode());
          $body = json_decode((string) $response->getBody());
          $this->assertEquals('Not authorized: Malformed JWT token', $body->message);
-         $this->assertContains('ApiAuthorizationException', $body->stacktrace);
+         $this->assertContainsPolyfill('ApiAuthorizationException', $body->stacktrace);
      }
  }
index 0d07897b40ee16913206aab94dcf87dd6b74c3af,a708795381f21925bae4609656048a979644f2d1..ef00b92f8c3bce554b2dd56e518fa27bb4578a59
@@@ -2,7 -2,7 +2,7 @@@
  
  namespace Shaarli\Bookmark;
  
- use PHPUnit\Framework\TestCase;
+ use Shaarli\TestCase;
  
  require_once 'tests/utils/CurlUtils.php';
  
@@@ -42,19 -42,6 +42,19 @@@ class LinkUtilsTest extends TestCas
          $this->assertEquals(strtolower($charset), header_extract_charset($headers));
      }
  
 +    /**
 +     * Test headers_extract_charset() when the charset is found with odd quotes.
 +     */
 +    public function testHeadersExtractExistentCharsetWithQuotes()
 +    {
 +        $charset = 'x-MacCroatian';
 +        $headers = 'text/html; charset="' . $charset . '"otherstuff="test"';
 +        $this->assertEquals(strtolower($charset), header_extract_charset($headers));
 +
 +        $headers = 'text/html; charset=\'' . $charset . '\'otherstuff="test"';
 +        $this->assertEquals(strtolower($charset), header_extract_charset($headers));
 +    }
 +
      /**
       * Test headers_extract_charset() when the charset is not found.
       */
              カタカナ #カタカナ」カタカナ\n';
          $autolinkedDescription = hashtag_autolink($rawDescription, $index);
  
-         $this->assertContains($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
-         $this->assertNotContains(' #hashtag', $autolinkedDescription);
-         $this->assertNotContains('>#nothashtag', $autolinkedDescription);
-         $this->assertContains($this->getHashtagLink('ашок', $index), $autolinkedDescription);
-         $this->assertContains($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
-         $this->assertContains($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
-         $this->assertNotContains($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
+         $this->assertContainsPolyfill($this->getHashtagLink('hashtag', $index), $autolinkedDescription);
+         $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
+         $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
+         $this->assertContainsPolyfill($this->getHashtagLink('ашок', $index), $autolinkedDescription);
+         $this->assertContainsPolyfill($this->getHashtagLink('カタカナ', $index), $autolinkedDescription);
+         $this->assertContainsPolyfill($this->getHashtagLink('hashtag_hashtag', $index), $autolinkedDescription);
+         $this->assertNotContainsPolyfill($this->getHashtagLink('hashtag-nothashtag', $index), $autolinkedDescription);
      }
  
      /**
          $rawDescription = 'blabla #hashtag x#nothashtag';
          $autolinkedDescription = hashtag_autolink($rawDescription);
  
-         $this->assertContains($this->getHashtagLink('hashtag'), $autolinkedDescription);
-         $this->assertNotContains(' #hashtag', $autolinkedDescription);
-         $this->assertNotContains('>#nothashtag', $autolinkedDescription);
+         $this->assertContainsPolyfill($this->getHashtagLink('hashtag'), $autolinkedDescription);
+         $this->assertNotContainsPolyfill(' #hashtag', $autolinkedDescription);
+         $this->assertNotContainsPolyfill('>#nothashtag', $autolinkedDescription);
      }
  
      /**
index a5e2dbc56c959bda171cd5677e2adbaadb97d409,3999b44e5bf546268d979a6a04fbf670560fb25e..f7a68226148e4b9434649b8fb409956745322416
@@@ -4,7 -4,6 +4,6 @@@ declare(strict_types=1)
  
  namespace Shaarli\Front\Controller\Admin\ManageShaareControllerTest;
  
- use PHPUnit\Framework\TestCase;
  use Shaarli\Bookmark\Bookmark;
  use Shaarli\Config\ConfigManager;
  use Shaarli\Front\Controller\Admin\FrontAdminControllerMockHelper;
@@@ -12,6 -11,7 +11,7 @@@ use Shaarli\Front\Controller\Admin\Mana
  use Shaarli\Front\Exception\WrongTokenException;
  use Shaarli\Http\HttpAccess;
  use Shaarli\Security\SessionManager;
+ use Shaarli\TestCase;
  use Shaarli\Thumbnailer;
  use Slim\Http\Request;
  use Slim\Http\Response;
@@@ -88,17 -88,18 +88,18 @@@ class SaveBookmarkTest extends TestCas
  
          // Make sure that PluginManager hook is triggered
          $this->container->pluginManager
-             ->expects(static::at(0))
+             ->expects(static::atLeastOnce())
              ->method('executeHooks')
+             ->withConsecutive(['save_link'])
              ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
-                 static::assertSame('save_link', $hook);
-                 static::assertSame($id, $data['id']);
-                 static::assertSame($parameters['lf_url'], $data['url']);
-                 static::assertSame($parameters['lf_title'], $data['title']);
-                 static::assertSame($parameters['lf_description'], $data['description']);
-                 static::assertSame($parameters['lf_tags'], $data['tags']);
-                 static::assertTrue($data['private']);
+                 if ('save_link' === $hook) {
+                     static::assertSame($id, $data['id']);
+                     static::assertSame($parameters['lf_url'], $data['url']);
+                     static::assertSame($parameters['lf_title'], $data['title']);
+                     static::assertSame($parameters['lf_description'], $data['description']);
+                     static::assertSame($parameters['lf_tags'], $data['tags']);
+                     static::assertTrue($data['private']);
+                 }
  
                  return $data;
              })
  
          // Make sure that PluginManager hook is triggered
          $this->container->pluginManager
-             ->expects(static::at(0))
+             ->expects(static::atLeastOnce())
              ->method('executeHooks')
+             ->withConsecutive(['save_link'])
              ->willReturnCallback(function (string $hook, array $data) use ($parameters, $id): array {
-                 static::assertSame('save_link', $hook);
-                 static::assertSame($id, $data['id']);
-                 static::assertSame($parameters['lf_url'], $data['url']);
-                 static::assertSame($parameters['lf_title'], $data['title']);
-                 static::assertSame($parameters['lf_description'], $data['description']);
-                 static::assertSame($parameters['lf_tags'], $data['tags']);
-                 static::assertTrue($data['private']);
+                 if ('save_link' === $hook) {
+                     static::assertSame($id, $data['id']);
+                     static::assertSame($parameters['lf_url'], $data['url']);
+                     static::assertSame($parameters['lf_title'], $data['title']);
+                     static::assertSame($parameters['lf_description'], $data['description']);
+                     static::assertSame($parameters['lf_tags'], $data['tags']);
+                     static::assertTrue($data['private']);
+                 }
  
                  return $data;
              })
          static::assertSame(302, $result->getStatusCode());
      }
  
 +    /**
 +     * Test save a bookmark - with ID #0
 +     */
 +    public function testSaveBookmarkWithIdZero(): void
 +    {
 +        $parameters = ['lf_id' => '0'];
 +
 +        $request = $this->createMock(Request::class);
 +        $request
 +            ->method('getParam')
 +            ->willReturnCallback(function (string $key) use ($parameters): ?string {
 +                return $parameters[$key] ?? null;
 +            })
 +        ;
 +        $response = new Response();
 +
 +        $this->container->bookmarkService->expects(static::once())->method('exists')->with(0)->willReturn(true);
 +        $this->container->bookmarkService->expects(static::once())->method('get')->with(0)->willReturn(new Bookmark());
 +
 +        $result = $this->controller->save($request, $response);
 +
 +        static::assertSame(302, $result->getStatusCode());
 +    }
 +
      /**
       * Change the password with a wrong existing password
       */