assertTrue(method_exists(TermNode::class, '__toString'), 'Class does not have method __toString'); $node = new TermNode('foo'); $this->assertEquals('', (string) $node); $node_with_context = new TermNode('foo', new Context('bar')); $this->assertEquals('', (string) $node_with_context); } public function testQueryBuild() { $field = new Field('foo', Mapping::TYPE_STRING, ['private' => false]); $query_context = $this->prophesize(QueryContext::class); $query_context ->getUnrestrictedFields() ->willReturn([$field]); $query_context ->getPrivateFields() ->willReturn([]); $node = new TermNode('bar'); $node->setConcepts([ new Concept('/baz'), new Concept('/qux'), ]); $query = $node->buildQuery($query_context->reveal()); $expected = '{ "bool": { "should": [{ "multi_match": { "fields": ["concept_path.foo"], "query": "/baz" } }, { "multi_match": { "fields": ["concept_path.foo"], "query": "/qux" } }] } }'; $this->assertEquals(json_decode($expected, true), $query); } public function testQueryBuildWithZeroConcept() { $field = new Field('foo', Mapping::TYPE_STRING, ['private' => false]); $query_context = $this->prophesize(QueryContext::class); $query_context ->getUnrestrictedFields() ->willReturn([$field]); $query_context ->getPrivateFields() ->willReturn([]); $node = new TermNode('bar'); $query = $node->buildQuery($query_context->reveal()); $expected = '{ "bool": { "should": [] } }'; $this->assertEquals(json_decode($expected, true), $query); } public function testQueryBuildWithPrivateFields() { $public_field = new Field('foo', Mapping::TYPE_STRING, ['private' => false]); $private_field = new Field('bar', Mapping::TYPE_STRING, [ 'private' => true, 'used_by_collections' => [1, 2, 3] ]); $query_context = $this->prophesize(QueryContext::class); $query_context ->getUnrestrictedFields() ->willReturn([$public_field]); $query_context ->getPrivateFields() ->willReturn([$private_field]); $node = new TermNode('baz'); $node->setConcepts([ new Concept('/baz'), new Concept('/qux'), ]); $query = $node->buildQuery($query_context->reveal()); $expected = '{ "bool": { "should": [{ "multi_match": { "fields": ["concept_path.foo"], "query": "/baz" } }, { "multi_match": { "fields": ["concept_path.foo"], "query": "/qux" } }, { "bool": { "must": [{ "terms": { "base_id": [1, 2, 3] } }], "should": [{ "multi_match": { "fields": ["concept_path.bar"], "query": "/baz" } }, { "multi_match": { "fields": ["concept_path.bar"], "query": "/qux" } }] } }] } }'; $this->assertEquals(json_decode($expected, true), $query); } }