| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // {{{ License
 | 
					
						
							| 
									
										
										
										
											2021-03-31 09:54:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | // This file is part of GNU social - https://www.gnu.org/software/social
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // GNU social 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 GNU social.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							| 
									
										
										
										
											2021-03-31 09:54:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | // }}} License
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * This file test the Macro that Embeds SVG icons. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @package   Tests | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author    Ângelo D. Moura <up201303828@fe.up.pt> | 
					
						
							|  |  |  |  * @copyright 2020 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @author    Hugo Sales <hugo@hsal.es> | 
					
						
							|  |  |  |  * @copyright 2021 Free Software Foundation, Inc http://www.fsf.org | 
					
						
							|  |  |  |  * @license   https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 18:26:00 +00:00
										 |  |  | namespace App\Tests\Twig; | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-05 13:37:10 +00:00
										 |  |  | use App\Core\DB\DB; | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | use App\Twig\Extension; | 
					
						
							|  |  |  | use App\Twig\Runtime; | 
					
						
							| 
									
										
										
										
											2021-05-06 21:56:28 +00:00
										 |  |  | use App\Util\GNUsocialTestCase; | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | use DirectoryIterator; | 
					
						
							| 
									
										
										
										
											2021-04-10 21:57:00 +00:00
										 |  |  | use Symfony\Component\HttpFoundation\Request; | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-06 21:56:28 +00:00
										 |  |  | class ExtensionTest extends GNUsocialTestCase | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     public function testIconsExtension() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         // Get all Icon files names from "public/assets/icons"
 | 
					
						
							|  |  |  |         $icon_file_names = []; | 
					
						
							|  |  |  |         foreach (new DirectoryIterator('public/assets/icons/') as $file) { | 
					
						
							|  |  |  |             if ($file->isDot()) { | 
					
						
							|  |  |  |                 continue; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             $icon_file_names[] = $file->getFilename(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 01:35:41 +01:00
										 |  |  |         // Check if every icon file has either ".svg.twig" extension or ".svg"
 | 
					
						
							|  |  |  |         $twig_icon_file_names = []; | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  |         foreach ($icon_file_names as $icon_file_name) { | 
					
						
							| 
									
										
										
										
											2021-08-12 01:35:41 +01:00
										 |  |  |             static::assertMatchesRegularExpression('/\.svg\.twig$|\.svg$/', $icon_file_name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             if (preg_match('/\.svg\.twig$/', $icon_file_name, $matches, PREG_OFFSET_CAPTURE, 0)) { | 
					
						
							|  |  |  |                 unset($matches); | 
					
						
							|  |  |  |                 $twig_icon_file_names[] = $icon_file_name; | 
					
						
							|  |  |  |             } | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-08-12 01:35:41 +01:00
										 |  |  |         unset($icon_file_names); | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         //Check if the function gives a valid HTML with a class attribute equal to the one passed
 | 
					
						
							|  |  |  |         static::bootKernel(); | 
					
						
							| 
									
										
										
										
											2021-03-31 15:12:01 +00:00
										 |  |  |         $container = self::$kernel->getContainer()->get('test.service_container'); | 
					
						
							|  |  |  |         $twig      = $container->get('twig'); | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-12 01:35:41 +01:00
										 |  |  |         foreach ($twig_icon_file_names as $icon_file_name) { | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  |             $icon_name = basename($icon_file_name, '.svg.twig'); | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  |             $icon_template_render = $twig->render('@public_path/assets/icons/' . $icon_file_name, ['iconClass' => 'icon icon-' . $icon_name]); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-31 09:54:50 +00:00
										 |  |  |             $icons_extension       = new Runtime(); | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  |             $icon_extension_render = $icons_extension->embedSvgIcon($twig, $icon_name, 'icon icon-' . $icon_name); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-09 19:43:39 +00:00
										 |  |  |             static::assertSame($icon_template_render, $icon_extension_render); | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-04-10 21:57:00 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testIsCurrentRouteActive() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-09-09 01:08:45 +01:00
										 |  |  |         $req = $this->createMock(Request::class); | 
					
						
							|  |  |  |         // @phpstan-ignore-next-line
 | 
					
						
							| 
									
										
										
										
											2021-04-10 21:57:00 +00:00
										 |  |  |         $req->attributes = new class { | 
					
						
							|  |  |  |             public function get(string $arg) | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |                 return 'current_route'; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         }; | 
					
						
							|  |  |  |         $runtime = new Runtime; | 
					
						
							|  |  |  |         $runtime->setRequest($req); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         static::assertSame('active', $runtime->isCurrentRouteActive('current_route')); | 
					
						
							|  |  |  |         static::assertSame('', $runtime->isCurrentRouteActive('some_route', 'some_other_route')); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-05-05 13:37:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public function testGetNoteActions() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         static::bootKernel(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $req     = $this->createMock(Request::class); | 
					
						
							|  |  |  |         $runtime = new Runtime; | 
					
						
							|  |  |  |         $runtime->setRequest($req); | 
					
						
							|  |  |  |         static::assertSame([], $runtime->getNoteActions(DB::dql('select n from note n where n.content = \'some content\'')[0])); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-20 23:38:56 +01:00
										 |  |  | } |