Change Elastic Field structure to cope with Collection base_ids

This commit is contained in:
Benoît Burnichon
2015-06-05 20:14:33 +02:00
parent acb8ed86c8
commit fea47ef5ee
4 changed files with 26 additions and 19 deletions

View File

@@ -20,6 +20,7 @@ class Field
private $is_private; private $is_private;
private $is_facet; private $is_facet;
private $thesaurus_roots; private $thesaurus_roots;
private $used_by_collections;
public static function createFromLegacyField(databox_field $field) public static function createFromLegacyField(databox_field $field)
{ {
@@ -54,9 +55,9 @@ class Field
case databox_field::TYPE_STRING: case databox_field::TYPE_STRING:
case databox_field::TYPE_TEXT: case databox_field::TYPE_TEXT:
return Mapping::TYPE_STRING; 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 = []) public function __construct($name, $type, array $options = [])
@@ -97,6 +98,11 @@ class Field
return $this->type; return $this->type;
} }
public function getCollections()
{
return $this->collections;
}
public function isSearchable() public function isSearchable()
{ {
return $this->is_searchable; return $this->is_searchable;

View File

@@ -208,6 +208,9 @@ class databox extends base
return $this->ord; return $this->ord;
} }
/**
* @return appbox
*/
public function get_appbox() public function get_appbox()
{ {
return $this->app['phraseanet.appbox']; return $this->app['phraseanet.appbox'];
@@ -235,10 +238,8 @@ class databox extends base
return $base_ids; return $base_ids;
} }
$conn = $this->app['phraseanet.appbox']->get_connection(); $conn = $this->get_appbox()->get_connection();
$sql = "SELECT b.base_id FROM bas b $sql = "SELECT b.base_id FROM bas b WHERE b.sbas_id = :sbas_id AND b.active = '1' ORDER BY b.ord ASC";
WHERE b.sbas_id = :sbas_id AND b.active = '1'
ORDER BY b.ord ASC";
$stmt = $conn->prepare($sql); $stmt = $conn->prepare($sql);
$stmt->execute([':sbas_id' => $this->id]); $stmt->execute([':sbas_id' => $this->id]);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC); $rs = $stmt->fetchAll(PDO::FETCH_ASSOC);

View File

@@ -14,8 +14,8 @@ class FieldTest extends \PHPUnit_Framework_TestCase
{ {
public function testBasicMerge() public function testBasicMerge()
{ {
$field = 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); $other = new Field('foo', Mapping::TYPE_STRING, ['used_by_collections' => ['3', '4']]);
$merged = $field->mergeWith($other); $merged = $field->mergeWith($other);
$this->assertInstanceOf(Field::class, $merged); $this->assertInstanceOf(Field::class, $merged);
$this->assertNotSame($field, $merged); $this->assertNotSame($field, $merged);
@@ -26,6 +26,7 @@ class FieldTest extends \PHPUnit_Framework_TestCase
$this->assertFalse($merged->isPrivate()); $this->assertFalse($merged->isPrivate());
$this->assertFalse($merged->isFacet()); $this->assertFalse($merged->isFacet());
$this->assertNull($merged->getThesaurusRoots()); $this->assertNull($merged->getThesaurusRoots());
$this->assertEquals(['1', '2', '3', '4'], $merged->getCollections());
} }
/** /**

View File

@@ -34,22 +34,21 @@ class StructureTest extends \PHPUnit_Framework_TestCase
$field->isPrivate()->willReturn(false); $field->isPrivate()->willReturn(false);
$field->isFacet()->willReturn(false); $field->isFacet()->willReturn(false);
$field->hasConceptInference()->willReturn(false); $field->hasConceptInference()->willReturn(false);
$field->getCollections()->willReturn(['1']);
$structure->add($field->reveal()); $structure->add($field->reveal());
$this->assertCount(1, $structure->getAllFields()); $this->assertCount(1, $structure->getAllFields());
$conflicting_field = $this->prophesize(Field::class); $conflicting_field = new Field('foo', Mapping::TYPE_STRING, ['2']);
$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();
$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) // Should still have only one (both have the same name)
$structure->add($dummy); $structure->add($conflicting_field);
$this->assertCount(1, $structure->getAllFields()); $this->assertCount(1, $fields = $structure->getAllFields());
$this->assertInternalType('array', $fields);
$this->assertSame($merged, reset($fields));
} }
public function testFieldMerge() public function testFieldMerge()
@@ -166,7 +165,7 @@ class StructureTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @expectedException DomainException * @expectedException \DomainException
* @expectedExceptionMessageRegExp #field#u * @expectedExceptionMessageRegExp #field#u
*/ */
public function testPrivateCheckWithInvalidField() public function testPrivateCheckWithInvalidField()