add base authorization to person search + improve search ordering

This commit is contained in:
2021-11-15 11:17:03 +00:00
parent 4ba93bb709
commit 8296d60cb6
22 changed files with 317 additions and 151 deletions

View File

@@ -2,23 +2,32 @@
declare(strict_types=1);
namespace Chill\PersonBundle\Tests\Controller;
use Chill\MainBundle\Test\PrepareClientTrait;
use Chill\PersonBundle\DataFixtures\Helper\PersonRandomHelper;
use Chill\PersonBundle\Entity\Person;
use Chill\PersonBundle\Entity\Relationships\Relation;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\HttpFoundation\Request;
use Chill\PersonBundle\Repository\PersonRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class RelationshipApiControllerTest extends WebTestCase
{
public static function setUpBeforeClass()
{
static::bootKernel();
}
use PrepareClientTrait;
private KernelBrowser $client;
/**
* A cache for all relations
* @var array|null|Relation[]
*/
private ?array $relations = null;
public function setUp()
{
$this->client = static::createClient(array(), array(
'PHP_AUTH_USER' => 'fred',
'PHP_AUTH_PW' => 'password',
));
static::bootKernel();
$this->client = $this->getClientAuthenticated();
}
/**
@@ -38,11 +47,11 @@ class RelationshipApiControllerTest extends WebTestCase
public function testPostRelationship($fromPersonId, $toPersonId, $relationId, $isReverse): void
{
$this->client->request(Request::METHOD_POST,
'/api/1.0/person/relations/relationship.json',
$this->client->request(Request::METHOD_POST,
'/api/1.0/relations/relationship.json',
[],
[],
[],
[],
\json_encode([
'type' => 'relationship',
'fromPerson' => ['id' => $fromPersonId, 'type' => 'person'],
@@ -57,18 +66,72 @@ class RelationshipApiControllerTest extends WebTestCase
public function relationProvider(): array
{
//TODO: which different cases to test?
static::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$countPersons = $em->createQueryBuilder()
->select('count(p)')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->getSingleScalarResult()
;
$persons = $em->createQueryBuilder()
->select('p')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->setMaxResults(2)
->setFirstResult(\random_int(0, $countPersons - 1))
->getResult()
;
return [
[333, 334, 1, true],
[$persons[0]->getId(), $persons[1]->getId(), $this->getRandomRelation($em)->getId(), true],
];
}
private function getRandomRelation(EntityManagerInterface $em): Relation
{
if (null === $this->relations) {
$this->relations = $em->getRepository(Relation::class)
->findAll();
}
return $this->relations[\array_rand($this->relations)];
}
public function personProvider(): array
{
//TODO: which different cases to test?
static::bootKernel();
$em = self::$container->get(EntityManagerInterface::class);
$countPersons = $em->createQueryBuilder()
->select('count(p)')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->getSingleScalarResult()
;
$person = $em->createQueryBuilder()
->select('p')
->from(Person::class, 'p')
->join('p.center', 'c')
->where('c.name LIKE :name')
->setParameter('name', 'Center A')
->getQuery()
->setMaxResults(1)
->setFirstResult(\random_int(0, $countPersons - 1))
->getSingleResult()
;
return [
[333],
[$person->getId()],
];
}
}
}