mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-16 14:33:14 +00:00
Fix getUnrestrictedFields() when called on non narrowed query context
Add tests and prevent AST to rely on field key by returning indexed arrays instead of associative ones.
This commit is contained in:
@@ -48,7 +48,7 @@ class QueryContext
|
||||
}
|
||||
|
||||
$fields = array();
|
||||
foreach ($this->getUnrestrictedFields() as $name => $field) {
|
||||
foreach ($this->getUnrestrictedFields() as $field) {
|
||||
$fields[] = $field->getIndexField(true);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ class QueryContext
|
||||
}
|
||||
|
||||
$fields = array();
|
||||
foreach ($this->getUnrestrictedFields() as $_ => $field) {
|
||||
foreach ($this->getUnrestrictedFields() as $field) {
|
||||
foreach ($this->localizeField($field) as $fields[]);
|
||||
}
|
||||
|
||||
@@ -73,20 +73,22 @@ class QueryContext
|
||||
{
|
||||
// TODO Restore search optimization by using "caption_all" field
|
||||
// (only when $this->fields is null)
|
||||
return array_intersect_key(
|
||||
$this->structure->getUnrestrictedFields(),
|
||||
array_flip($this->fields)
|
||||
);
|
||||
$fields = $this->structure->getUnrestrictedFields();
|
||||
if ($this->fields !== null) {
|
||||
$fields = array_intersect_key($fields, array_flip($this->fields));
|
||||
}
|
||||
|
||||
return array_values($fields);
|
||||
}
|
||||
|
||||
public function getPrivateFields()
|
||||
{
|
||||
$private_fields = $this->structure->getPrivateFields();
|
||||
if ($this->fields === null) {
|
||||
return $private_fields;
|
||||
} else {
|
||||
return array_intersect_key($private_fields, array_flip($this->fields));
|
||||
$fields = $this->structure->getPrivateFields();
|
||||
if ($this->fields !== null) {
|
||||
$fields = array_intersect_key($fields, array_flip($this->fields));
|
||||
}
|
||||
|
||||
return array_values($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -34,4 +34,38 @@ class QueryContextTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals('caption.foo', $context->normalizeField('foo'));
|
||||
$this->assertEquals('private_caption.bar', $context->normalizeField('bar'));
|
||||
}
|
||||
|
||||
public function testGetUnrestrictedFields()
|
||||
{
|
||||
$foo_field = new Field('foo', Mapping::TYPE_STRING, ['private' => false]);
|
||||
$bar_field = new Field('bar', Mapping::TYPE_STRING, ['private' => false]);
|
||||
$structure = $this->prophesize(Structure::class);
|
||||
$structure->getUnrestrictedFields()->willReturn([
|
||||
'foo' => $foo_field,
|
||||
'bar' => $bar_field
|
||||
]);
|
||||
|
||||
$context = new QueryContext($structure->reveal(), [], 'fr');
|
||||
$this->assertEquals([$foo_field, $bar_field], $context->getUnrestrictedFields());
|
||||
|
||||
$narrowed_context = new QueryContext($structure->reveal(), [], 'fr', ['foo']);
|
||||
$this->assertEquals([$foo_field], $narrowed_context->getUnrestrictedFields());
|
||||
}
|
||||
|
||||
public function testGetPrivateFields()
|
||||
{
|
||||
$foo_field = new Field('foo', Mapping::TYPE_STRING, ['private' => true]);
|
||||
$bar_field = new Field('bar', Mapping::TYPE_STRING, ['private' => true]);
|
||||
$structure = $this->prophesize(Structure::class);
|
||||
$structure->getPrivateFields()->willReturn([
|
||||
'foo' => $foo_field,
|
||||
'bar' => $bar_field
|
||||
]);
|
||||
|
||||
$context = new QueryContext($structure->reveal(), [], 'fr');
|
||||
$this->assertEquals([$foo_field, $bar_field], $context->getPrivateFields());
|
||||
|
||||
$narrowed_context = new QueryContext($structure->reveal(), [], 'fr', ['foo']);
|
||||
$this->assertEquals([$foo_field], $narrowed_context->getPrivateFields());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user