diff --git a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php index d7b32b0108..1922959501 100644 --- a/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php +++ b/lib/Alchemy/Phrasea/SearchEngine/Elastic/Structure/Field.php @@ -20,6 +20,7 @@ class Field private $is_private; private $is_facet; private $thesaurus_roots; + private $used_by_collections; public static function createFromLegacyField(databox_field $field) { @@ -54,9 +55,9 @@ class Field case databox_field::TYPE_STRING: case databox_field::TYPE_TEXT: return Mapping::TYPE_STRING; - default: - throw new Exception(sprintf('Invalid field type "%s", expected "date", "number" or "string".', $type)); } + + throw new \InvalidArgumentException(sprintf('Invalid field type "%s", expected "date", "number" or "string".', $type)); } public function __construct($name, $type, array $options = []) @@ -97,6 +98,11 @@ class Field return $this->type; } + public function getCollections() + { + return $this->collections; + } + public function isSearchable() { return $this->is_searchable; diff --git a/lib/classes/databox.php b/lib/classes/databox.php index fa6d8d5c0a..6a5d049e95 100644 --- a/lib/classes/databox.php +++ b/lib/classes/databox.php @@ -208,6 +208,9 @@ class databox extends base return $this->ord; } + /** + * @return appbox + */ public function get_appbox() { return $this->app['phraseanet.appbox']; @@ -235,10 +238,8 @@ class databox extends base return $base_ids; } - $conn = $this->app['phraseanet.appbox']->get_connection(); - $sql = "SELECT b.base_id FROM bas b - WHERE b.sbas_id = :sbas_id AND b.active = '1' - ORDER BY b.ord ASC"; + $conn = $this->get_appbox()->get_connection(); + $sql = "SELECT b.base_id FROM bas b WHERE b.sbas_id = :sbas_id AND b.active = '1' ORDER BY b.ord ASC"; $stmt = $conn->prepare($sql); $stmt->execute([':sbas_id' => $this->id]); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC); diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/FieldTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/FieldTest.php index 2e8575c0d3..3a5e49dd77 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/FieldTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/FieldTest.php @@ -14,8 +14,8 @@ class FieldTest extends \PHPUnit_Framework_TestCase { public function testBasicMerge() { - $field = new Field('foo', Mapping::TYPE_STRING); - $other = new Field('foo', Mapping::TYPE_STRING); + $field = new Field('foo', Mapping::TYPE_STRING, ['used_by_collections' => ['1', '2']]); + $other = new Field('foo', Mapping::TYPE_STRING, ['used_by_collections' => ['3', '4']]); $merged = $field->mergeWith($other); $this->assertInstanceOf(Field::class, $merged); $this->assertNotSame($field, $merged); @@ -26,6 +26,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase $this->assertFalse($merged->isPrivate()); $this->assertFalse($merged->isFacet()); $this->assertNull($merged->getThesaurusRoots()); + $this->assertEquals(['1', '2', '3', '4'], $merged->getCollections()); } /** diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/StructureTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/StructureTest.php index 46b79929d7..93bd0fb042 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/StructureTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/Structure/StructureTest.php @@ -34,22 +34,21 @@ class StructureTest extends \PHPUnit_Framework_TestCase $field->isPrivate()->willReturn(false); $field->isFacet()->willReturn(false); $field->hasConceptInference()->willReturn(false); + $field->getCollections()->willReturn(['1']); $structure->add($field->reveal()); $this->assertCount(1, $structure->getAllFields()); - $conflicting_field = $this->prophesize(Field::class); - $conflicting_field->getName()->willReturn('foo'); - $conflicting_field->getType()->willReturn(Mapping::TYPE_STRING); - $conflicting_field->isPrivate()->willReturn(false); - $conflicting_field->isFacet()->willReturn(false); - $conflicting_field->hasConceptInference()->willReturn(false); - $dummy = $conflicting_field->reveal(); + $conflicting_field = new Field('foo', Mapping::TYPE_STRING, ['2']); - $field->mergeWith($dummy)->willReturn($dummy); + $merged = new Field('foo', Mapping::TYPE_STRING, ['1', '2']); + + $field->mergeWith($conflicting_field)->willReturn($merged); // Should still have only one (both have the same name) - $structure->add($dummy); - $this->assertCount(1, $structure->getAllFields()); + $structure->add($conflicting_field); + $this->assertCount(1, $fields = $structure->getAllFields()); + $this->assertInternalType('array', $fields); + $this->assertSame($merged, reset($fields)); } public function testFieldMerge() @@ -166,7 +165,7 @@ class StructureTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException DomainException + * @expectedException \DomainException * @expectedExceptionMessageRegExp #field#u */ public function testPrivateCheckWithInvalidField()