ExtensionTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. // {{{ License
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. // }}} License
  18. /**
  19. * This file test the Macro that Embeds SVG icons.
  20. *
  21. * @package Tests
  22. *
  23. * @author Ângelo D. Moura <up201303828@fe.up.pt>
  24. * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org
  25. * @author Hugo Sales <hugo@hsal.es>
  26. * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. */
  29. namespace App\Tests\Twig;
  30. use App\Core\DB\DB;
  31. use App\Twig\Extension;
  32. use App\Twig\Runtime;
  33. use App\Util\GNUsocialTestCase;
  34. use DirectoryIterator;
  35. use Symfony\Component\HttpFoundation\Request;
  36. class ExtensionTest extends GNUsocialTestCase
  37. {
  38. public function testIconsExtension()
  39. {
  40. // Get all Icon files names from "public/assets/icons"
  41. $icon_file_names = [];
  42. foreach (new DirectoryIterator('public/assets/icons/') as $file) {
  43. if ($file->isDot()) {
  44. continue;
  45. }
  46. $icon_file_names[] = $file->getFilename();
  47. }
  48. //Check if every icon file as a ".svg.twig" extension
  49. foreach ($icon_file_names as $icon_file_name) {
  50. static::assertMatchesRegularExpression('/.svg.twig/', $icon_file_name);
  51. }
  52. //Check if the function gives a valid HTML with a class attribute equal to the one passed
  53. static::bootKernel();
  54. $container = self::$kernel->getContainer()->get('test.service_container');
  55. $twig = $container->get('twig');
  56. foreach ($icon_file_names as $icon_file_name) {
  57. $icon_name = basename($icon_file_name, '.svg.twig');
  58. $icon_template_render = $twig->render('@public_path/assets/icons/' . $icon_file_name, ['iconClass' => 'icon icon-' . $icon_name]);
  59. $icons_extension = new Runtime();
  60. $icon_extension_render = $icons_extension->embedSvgIcon($twig, $icon_name, 'icon icon-' . $icon_name);
  61. static::assertSame($icon_template_render, $icon_extension_render);
  62. }
  63. }
  64. public function testIsCurrentRouteActive()
  65. {
  66. $req = $this->createMock(Request::class);
  67. $req->attributes = new class {
  68. public function get(string $arg)
  69. {
  70. return 'current_route';
  71. }
  72. };
  73. $runtime = new Runtime;
  74. $runtime->setRequest($req);
  75. static::assertSame('active', $runtime->isCurrentRouteActive('current_route'));
  76. static::assertSame('', $runtime->isCurrentRouteActive('some_route', 'some_other_route'));
  77. }
  78. public function testGetNoteActions()
  79. {
  80. static::bootKernel();
  81. $req = $this->createMock(Request::class);
  82. $runtime = new Runtime;
  83. $runtime->setRequest($req);
  84. static::assertSame([], $runtime->getNoteActions(DB::dql('select n from note n where n.content = \'some content\'')[0]));
  85. }
  86. }