/**
  * Unit test for eZContentFunctionCollection::fetchReverseRelatedObjectsCount
  */
 public function testFetchReverseRelatedObjectsCount()
 {
     $object1 = new ezpObject('article', 2);
     $object1->title = __FUNCTION__ . ' A';
     $object1->publish();
     $object2 = new ezpObject('article', 2);
     $object2->title = __FUNCTION__ . ' B';
     $object2->addContentObjectRelation($object1->attribute('id'));
     $object2->publish();
     $ret = eZContentFunctionCollection::fetchReverseRelatedObjectsCount($object1->attribute('id'), false, true, false);
     $this->assertInternalType('array', $ret);
     $this->assertArrayHasKey('result', $ret);
     $this->assertEquals(1, $ret['result']);
 }
 /**
  * Unit test for eZContentObject::relatedObjectCount()
  *
  * Outline:
  * 1) Create a content class with ezobjectrelation and ezobjectrelationlist
  *    attributes
  * 2) Create objects and relate them to each of these attributes and to the
  *    object itself (common)
  * 3) Check that attribute count is correct on each attribute and globally
  */
 public function testRelatedObjectCount()
 {
     // Create a test content class
     $class = new ezpClass(__FUNCTION__, __FUNCTION__, 'name');
     $class->add('Name', 'name', 'ezstring');
     $attributes['single_relation_1'] = $class->add('Single relation #1', 'single_relation_1', 'ezobjectrelation')->attribute('id');
     $attributes['single_relation_2'] = $class->add('Single relation #2', 'single_relation_2', 'ezobjectrelation')->attribute('id');
     $attributes['multiple_relations_1'] = $class->add('Multiple relations #1', 'multiple_relations_1', 'ezobjectrelationlist')->attribute('id');
     $attributes['multiple_relations_2'] = $class->add('Multiple relations #2', 'multiple_relations_2', 'ezobjectrelationlist')->attribute('id');
     $class->store();
     // create a few articles we will relate our object to
     $relatedObjects = array();
     for ($i = 0; $i < 10; $i++) {
         $article = new ezpObject('article', 2);
         $article->title = "Related object #'{$i} for " . __FUNCTION__;
         $article->publish();
         $relatedObjects[] = $article->attribute('id');
     }
     // Create a test object with various relations (some objects are related
     // to multiple attributes in order to test reverse relations):
     // - 1 relation (IDX 0) on single_relation_1
     // - 1 relation (IDX 1) on single_relation_2
     // - 2 relations (IDX 2, 3) on multiple_relations_1
     // - 2 relations (IDX 4, 5) on multiple_relations_2
     // - 6 object level relations ((IDX 6, 7, 8, 9)
     $object = new ezpObject(__FUNCTION__, 2);
     $object->name = __FUNCTION__;
     $object->single_relation_1 = $relatedObjects[0];
     $object->single_relation_2 = $relatedObjects[1];
     $object->multiple_relations_1 = array($relatedObjects[0], $relatedObjects[2], $relatedObjects[3]);
     $object->multiple_relations_2 = array($relatedObjects[1], $relatedObjects[4], $relatedObjects[5]);
     $object->addContentObjectRelation($relatedObjects[0]);
     $object->addContentObjectRelation($relatedObjects[1]);
     $object->addContentObjectRelation($relatedObjects[6]);
     $object->addContentObjectRelation($relatedObjects[7]);
     $object->addContentObjectRelation($relatedObjects[8]);
     $object->addContentObjectRelation($relatedObjects[9]);
     $object->publish();
     // Create 2 more objects with relations to $relatedObjects[9]
     // in order to test reverse related objects
     $otherObject1 = new ezpObject(__FUNCTION__, 2);
     $otherObject1->name = "Reverse test object #1 for " . __FUNCTION__;
     $otherObject1->single_relation_1 = $relatedObjects[9];
     $otherObject1->publish();
     $otherObject2 = new ezpObject(__FUNCTION__, 2);
     $otherObject2->name = "Reverse test object #2 for " . __FUNCTION__;
     $otherObject2->single_relation_2 = $relatedObjects[9];
     $otherObject2->publish();
     $contentObject = eZContentObject::fetch($object->attribute('id'));
     $paramAllRelations = array('AllRelations' => true);
     $paramAttributeRelations = array('AllRelations' => eZContentObject::RELATION_ATTRIBUTE);
     $paramCommonRelations = array('AllRelations' => eZContentObject::RELATION_COMMON);
     // Test overall relation count
     $this->assertEquals(14, $contentObject->relatedObjectCount(false, false, false, $paramAllRelations), "Overall relation count should be 14");
     // Test relation count for each attribute
     $this->assertEquals(1, $contentObject->relatedObjectCount(false, $attributes['single_relation_1'], false, $paramAttributeRelations), "Relation count on attribute single_relation_1 should be 1");
     $this->assertEquals(1, $contentObject->relatedObjectCount(false, $attributes['single_relation_2'], false, $paramAttributeRelations), "Relation count on attribute single_relation_2 should be 1");
     $this->assertEquals(3, $contentObject->relatedObjectCount(false, $attributes['multiple_relations_1'], false, $paramAttributeRelations), "Relation count on attribute multiple_relations_1 should be 3");
     $this->assertEquals(3, $contentObject->relatedObjectCount(false, $attributes['multiple_relations_2'], false, $paramAttributeRelations), "Relation count on attribute multiple_relations_2 should be 3");
     // Test common level relation count
     $this->assertEquals(6, $contentObject->relatedObjectCount(false, false, false, $paramCommonRelations), "Common relations count should be 6");
     // Test reverse relation count on $relatedObject[9]
     // This object is related to:
     // - the main $object on common level
     // - one object on single_relation_1
     // - another object on single_relation_2
     $relatedContentObject = eZContentObject::fetch($relatedObjects[9]);
     $this->assertEquals(3, $relatedContentObject->relatedObjectCount(false, false, true, $paramAllRelations), "Overall reverse relation count should be 3");
     $this->assertEquals(1, $relatedContentObject->relatedObjectCount(false, false, true, $paramCommonRelations), "Common reverse relation count should be 1");
     $this->assertEquals(1, $relatedContentObject->relatedObjectCount(false, $attributes['single_relation_1'], true, $paramAttributeRelations), "Attribute reverse relation count on single_relation_1 should be 1");
     $this->assertEquals(1, $relatedContentObject->relatedObjectCount(false, $attributes['single_relation_2'], true, $paramAttributeRelations), "Attribute reverse relation count on single_relation_2 should be 1");
     // Test that trashed objects are not counted as related (issue #15142)
     $trashObject = eZContentObject::fetch($relatedObjects[9]);
     $trashObject->removeThis();
     $this->assertEquals(13, $contentObject->relatedObjectCount(false, false, false, $paramAllRelations), "Relation count after move to trash should be 13");
     // Empty the trash
     foreach (eZContentObjectTrashNode::trashList() as $node) {
         eZContentObjectTrashNode::purgeForObject($node->attribute('contentobject_id'));
     }
 }