2020-01-02 18:45:41 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-01-05 15:55:16 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-02 18:45:41 +01:00
|
|
|
namespace App\Tests\Services\Trees;
|
|
|
|
|
|
|
|
use App\Entity\Attachments\AttachmentType;
|
2020-01-02 22:55:28 +01:00
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Helpers\Trees\TreeViewNode;
|
2020-01-02 18:45:41 +01:00
|
|
|
use App\Services\Trees\TreeViewGenerator;
|
2020-01-02 22:55:28 +01:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2020-01-02 18:45:41 +01:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2020-01-02 22:55:28 +01:00
|
|
|
class TreeViewGeneratorTest extends WebTestCase
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
2020-01-05 22:49:00 +01:00
|
|
|
protected $em;
|
2020-01-02 18:45:41 +01:00
|
|
|
/**
|
|
|
|
* @var TreeViewGenerator
|
|
|
|
*/
|
|
|
|
protected $service;
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
protected function setUp(): void
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
|
|
|
parent::setUp(); // TODO: Change the autogenerated stub
|
|
|
|
|
|
|
|
//Get an service instance.
|
|
|
|
self::bootKernel();
|
2022-12-18 19:45:04 +01:00
|
|
|
$this->service = self::getContainer()->get(TreeViewGenerator::class);
|
|
|
|
$this->em = self::getContainer()->get(EntityManagerInterface::class);
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetGenericTree(): void
|
2020-01-02 18:45:41 +01:00
|
|
|
{
|
|
|
|
$tree = $this->service->getGenericTree(AttachmentType::class, null);
|
|
|
|
|
|
|
|
$this->assertIsArray($tree);
|
|
|
|
$this->assertContainsOnlyInstancesOf(TreeViewNode::class, $tree);
|
|
|
|
|
|
|
|
$this->assertCount(3, $tree);
|
|
|
|
$this->assertCount(2, $tree[0]->getNodes());
|
|
|
|
$this->assertCount(1, $tree[0]->getNodes()[0]->getNodes());
|
|
|
|
$this->assertEmpty($tree[2]->getNodes());
|
|
|
|
$this->assertEmpty($tree[1]->getNodes()[0]->getNodes());
|
|
|
|
|
|
|
|
//Check text
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('Node 1', $tree[0]->getText());
|
|
|
|
$this->assertSame('Node 2', $tree[1]->getText());
|
|
|
|
$this->assertSame('Node 3', $tree[2]->getText());
|
|
|
|
$this->assertSame('Node 1.1', $tree[0]->getNodes()[0]->getText());
|
|
|
|
$this->assertSame('Node 1.1.1', $tree[0]->getNodes()[0]->getNodes()[0]->getText());
|
2020-01-02 18:45:41 +01:00
|
|
|
|
|
|
|
//Check that IDs were set correctly
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame(1, $tree[0]->getId());
|
2023-01-30 23:40:22 +01:00
|
|
|
$this->assertSame(5, $tree[1]->getId());
|
|
|
|
$this->assertSame(3, $tree[0]->getNodes()[0]->getNodes()[0]->getId());
|
2020-01-02 22:55:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetTreeViewBasic(): void
|
2020-01-02 22:55:28 +01:00
|
|
|
{
|
|
|
|
$tree = $this->service->getTreeView(Category::class);
|
|
|
|
$this->assertIsArray($tree);
|
|
|
|
$this->assertContainsOnlyInstancesOf(TreeViewNode::class, $tree);
|
|
|
|
|
|
|
|
$this->assertCount(3, $tree);
|
|
|
|
$this->assertCount(2, $tree[0]->getNodes());
|
|
|
|
$this->assertCount(1, $tree[0]->getNodes()[0]->getNodes());
|
|
|
|
|
|
|
|
//Assert that the nodes contain the correct links
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('/en/category/1/parts', $tree[0]->getHref());
|
|
|
|
$this->assertSame('/en/category/2/parts', $tree[1]->getHref());
|
|
|
|
$this->assertSame('/en/category/7/parts', $tree[0]->getNodes()[0]->getNodes()[0]->getHref());
|
2020-01-02 22:55:28 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetTreeViewNewEdit(): void
|
2020-01-02 22:55:28 +01:00
|
|
|
{
|
|
|
|
$tree = $this->service->getTreeView(Category::class, null, 'newEdit');
|
|
|
|
|
|
|
|
//First element should link to new category
|
|
|
|
$this->assertStringContainsStringIgnoringCase('New', $tree[0]->getText());
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('/en/category/new', $tree[0]->getHref());
|
2020-01-02 22:55:28 +01:00
|
|
|
//By default the new element node is selected
|
|
|
|
$this->assertTrue($tree[0]->getState()->getSelected());
|
|
|
|
|
|
|
|
//Next element is spacing
|
2020-01-05 15:55:16 +01:00
|
|
|
$this->assertSame('', $tree[1]->getText());
|
2020-01-02 22:55:28 +01:00
|
|
|
$this->assertTrue($tree[1]->getState()->getDisabled());
|
|
|
|
|
|
|
|
//All other elements should be normal
|
|
|
|
$this->assertCount(5, $tree);
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:55:16 +01:00
|
|
|
public function testGetTreeViewSelectedNode(): void
|
2020-01-02 22:55:28 +01:00
|
|
|
{
|
|
|
|
$selected = $this->em->find(Category::class, 2);
|
|
|
|
$tree = $this->service->getTreeView(Category::class, null, 'edit', $selected);
|
2020-01-02 18:45:41 +01:00
|
|
|
|
2020-01-02 22:55:28 +01:00
|
|
|
$this->assertNull($tree[0]->getState());
|
|
|
|
//Only second element must be selected
|
|
|
|
$this->assertTrue($tree[1]->getState()->getSelected());
|
2020-01-02 18:45:41 +01:00
|
|
|
}
|
|
|
|
}
|