/**
  * 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']);
 }
    /**
     * @group issue18249
     */
    public function testIssue18249()
    {

        // setup a class C1 with objectrelationlist and pattern name
        $classIdentifier = strtolower( __FUNCTION__ );
        $class1 = new ezpClass(
            __FUNCTION__, $classIdentifier, '<test_name><test_relation>'
        );
        $class1->add( 'Test name', 'test_name', 'ezstring' );
        $class1->add( 'Test Relation', 'test_relation', 'ezobjectrelationlist' );
        $class1->store();

        $o1 = new ezpObject( 'article', 2, 14, 1, 'eng-GB' );
        $o1->title = 'Test_ENG';
        $o1->publish();
        $o1->addTranslation( 'xxx-XX', array( 'title' => 'Test_XXX' ) );

        $o2 = new ezpObject( $classIdentifier, 2, 14, 1, 'xxx-XX' );
        $o2->test_name = 'name_';
        $o2->test_relation = array( $o1->attribute( 'id' ) );
        $o2->publish();

        // test O2's name
        $this->assertEquals( 'name_Test_XXX', $o2->name );
    }
 public function testUpdateAndPublishObject()
 {
     // create content object first
     $object = new ezpObject("folder", 2);
     $object->title = __FUNCTION__ . '::' . __LINE__ . '::' . time();
     $object->publish();
     $contentObjectID = $object->attribute('id');
     if ($object instanceof eZContentObject) {
         $now = date('Y/m/d H:i:s', time());
         $sectionID = 3;
         $remoteID = md5($now);
         $attributeList = array('name' => 'name ' . $now, 'short_name' => 'short_name ' . $now, 'short_description' => 'short_description' . $now, 'description' => 'description' . $now, 'show_children' => false);
         $params = array();
         $params['attributes'] = $attributeList;
         $params['remote_id'] = $remoteID;
         $params['section_id'] = $sectionID;
         $result = eZContentFunctions::updateAndPublishObject($object, $params);
         $this->assertTrue($result);
         $object = eZContentObject::fetch($contentObjectID);
         $this->assertEquals($object->attribute('section_id'), $sectionID);
         $this->assertEquals($object->attribute('remote_id'), $remoteID);
         $dataMap = $object->dataMap();
         $this->assertEquals($attributeList['name'], $dataMap['name']->content());
         $this->assertEquals($attributeList['short_name'], $dataMap['short_name']->content());
         $this->assertEquals($attributeList['short_description'], $dataMap['short_description']->content());
         $this->assertEquals($attributeList['description'], $dataMap['description']->content());
         $this->assertEquals($attributeList['show_children'], (bool) $dataMap['show_children']->content());
     }
 }
 /**
  * @param string $name
  * @return eZContentObject
  */
 protected function createImage($name)
 {
     $imageObject = new ezpObject(self::CLASS_IDENTIFIER, 2);
     $imageObject->name = $name;
     $imageObject->image = self::IMAGE_FILE_PATH;
     $imageObject->publish();
     return $this->forceFetchContentObject($imageObject->attribute('id'));
 }
 /**
  * Creates an article in eZ Publish in the folder $folderId,
  * with title $articleTitle and summary $articleIntro and returns its ID.
  *
  * @param int $folderId
  * @param string $articleName
  * @param string $articleIntro
  * @return int The ID of the article created
  */
 public function createEZPArticle($folderId, $articleTitle, $articleIntro)
 {
     $object = new ezpObject('article', $folderId);
     $object->title = $articleTitle;
     $object->intro = $articleIntro;
     $object->publish();
     $objectId = (int) $object->attribute('id');
     return $objectId;
 }
 public function testIssue18249()
 {
     // setup a class C1 with objectrelationlist and pattern name
     $class1 = new ezpClass('TestClass1', 'testclass1', '<test_name><test_relation>');
     $class1->add('Test name', 'test_name', 'ezstring');
     $class1->add('Test Relation', 'test_relation', 'ezobjectrelationlist');
     $class1->store();
     // create an object O1 with eng and nor
     $o1 = new ezpObject('article', false, 14, 1, 'nor-NO');
     $o1->title = 'Test_NOR';
     $o1->publish();
     $o1->addTranslation('nor-NO', array('title' => 'Test_NOR'));
     // create an object O2 based on C1 with Nor
     $o2 = new ezpObject('testclass1', false, 14, 1, 'nor-NO');
     $o2->test_name = 'name_';
     $o2->test_relation = array($o1->attribute('id'));
     $o2->publish();
     // test O2's name
     $this->assertEquals('name_Test_NOR', $o2->name);
 }
    /**
     * Regression test for issue #15561:
     * eZContentObjectTreeNode::fetch() SQL error when conditions argument is given
     */
    public function testIssue15561()
    {
        // Create a new node so that we have a known object with a known ID
        $object = new ezpObject( 'article', 2 );
        $object->title = __FUNCTION__;
        $object->publish();
        $nodeID = $object->attribute( 'main_node_id' );

        $node = eZContentObjectTreeNode::fetch( $nodeID, false, true,
            array( 'contentobject_version' => 1 ) );

        $this->assertInstanceOf( 'eZContentObjectTreeNode', $node );
    }
 /**
  * Unit test for eZContentObject::relatedObjects()
  *
  * Outline:
  * 1) Create a content class with ezobjectrelation attribute
  * 2) Create object of that class and relate to another object through the attribute
  * 3) Check that object loaded by eZContentObject::relatedObjects() is the correct one
  */
 public function testRelatedObjectsWithAttributeId()
 {
     // Create a test content class
     $class = new ezpClass(__FUNCTION__, __FUNCTION__, 'name');
     $class->add('Name', 'name', 'ezstring');
     $attributeId = $class->add('Single relation #1', 'relation', 'ezobjectrelation')->attribute('id');
     $class->store();
     // Create an article we will relate our object to
     $article = new ezpObject('article', 2);
     $article->title = "Related object #1 for " . __FUNCTION__;
     $article->publish();
     // Create a test object with attribute relation to created article
     $object = new ezpObject(__FUNCTION__, 2);
     $object->name = __FUNCTION__;
     $object->relation = $article->attribute('id');
     $object->publish();
     $contentObject = eZContentObject::fetch($object->attribute('id'));
     $relatedObjects = $contentObject->relatedObjects(false, false, $attributeId);
     $this->assertCount(1, $relatedObjects);
     $this->assertInstanceOf("eZContentObject", $relatedObjects[0]);
     $this->assertEquals($article->attribute('id'), $relatedObjects[0]->attribute("id"), "Related object is not the expected object");
 }
    /**
     * Unit test for eZContentObjectTreeNode::getParentNodeIDListByContentObjectID()
     *
     * @todo test with $onlyMainNode=true
     * @todo test with $groupByObjectID=true
     */
    public function testGetParentNodeIdListByContentObjectID()
    {
        // Create a few containers with a few children below each
        $childrenIDArray = $childrenParentIDArray = array();
        for ( $i = 0; $i < 3; $i++ )
        {
            $folder = new ezpObject( 'folder', 2 );
            $folder->name = "Container #{$i} for " . __FUNCTION__;
            $folder->publish();

            $containerID = $folder->attribute( 'main_node_id' );
            $containerIDArray[] = $containerID;
            for ( $j = 0; $j < 5; $j++ )
            {
                $article = new ezpObject( 'article', $containerID );
                $article->title = "Object #{$i}";
                $article->publish();

                $articleID = $article->attribute( 'id' );
                $childrenIDArray[] = $articleID;
                $childrenParentIDArray[$articleID] = $containerID;
            }
        }

        // First test without grouping
        $parentNodeIDArray = eZContentObjectTreeNode::getParentNodeIdListByContentObjectID(
            $childrenIDArray, false );
        $this->assertEquals( count( $childrenIDArray ), count( $parentNodeIDArray ),
            "count of returned items doesn't match the parameters count" );
        foreach( $parentNodeIDArray as $parentNodeID )
        {
            $this->assertTrue( in_array( $parentNodeID, $containerIDArray ),
                "Returned parent_node_id $parentNodeID isn't in the list of created nodes" );
        }
    }
    /**
     * 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' ) );
        }
    }
 /**
  * test for getData()
  */
 public function testGetData()
 {
     $providerArray = array();
     #start 1 : the simplest attribute
     $content1 = "Hello world";
     $ezcca1 = new eZContentClassAttribute(array('identifier' => 'title', 'data_type_string' => 'ezstring'));
     $ezcoa1 = new eZContentObjectAttributeTester(array("data_type_string" => 'ezstring', "id" => 100123, "data_text" => $content1, "contentclass_attribute" => $ezcca1));
     $fieldName1 = ezfSolrDocumentFieldBase::getFieldName($ezcca1);
     $expectedData1 = array($fieldName1 => $content1);
     $providerArray[] = array($expectedData1, $ezcoa1);
     #end 1
     #start 2 : attribute with subattributes
     $ezcca2 = new eZContentClassAttribute(array('identifier' => 'dummy', 'data_type_string' => 'dummy_example'));
     $ezcoa2 = new eZContentObjectAttributeTester(array("data_type_string" => 'dummy_example', "id" => 100124, "contentclass_attribute" => $ezcca2));
     $fieldList2 = ezfSolrDocumentFieldBase::getFieldNameList($ezcca2);
     $expectedData2 = array();
     foreach ($fieldList2 as $fieldName) {
         $expectedData2[$fieldName] = 'DATA_FOR_' . $fieldName;
     }
     $fieldName2 = ezfSolrDocumentFieldBase::getFieldName($ezcca2);
     $providerArray[] = array($expectedData2, $ezcoa2);
     #end 2
     #start 3 : object relations
     $expectedData3 = array();
     $tester3 = new ezfSolrDocumentFieldObjectRelationTester(new eZContentObjectAttribute(array()));
     $time3 = time();
     $image3 = new ezpObject("image", 2);
     $image3->name = __METHOD__ . $time3;
     $image3->caption = __METHOD__ . $time3;
     $imageId3 = $image3->publish();
     // $image3->object->clearCache();
     $dataMapImage3 = $image3->dataMap;
     // eZContentObjectAttribute objects, attributes of the related Image
     $imageName3 = $dataMapImage3['name'];
     $imageCaption3 = $dataMapImage3['caption'];
     $article3 = new ezpObject("article", 2);
     $articleId3 = $article3->publish();
     // Create object relation
     $article3->object->addContentObjectRelation($imageId3, $article3->current_version, 154, eZContentObject::RELATION_ATTRIBUTE);
     $dataMapArticle3 = $article3->attribute('data_map');
     $ezcoa3 = $dataMapArticle3['image'];
     $ezcoa3->setAttribute('data_int', $imageId3);
     $ezcoa3->store();
     $ezcca3 = $ezcoa3->attribute('contentclass_attribute');
     $defaultFieldName3 = ezfSolrDocumentFieldBase::generateAttributeFieldName($ezcca3, ezfSolrDocumentFieldObjectRelation::$subattributesDefinition[ezfSolrDocumentFieldObjectRelation::DEFAULT_SUBATTRIBUTE]);
     $expectedData3[$defaultFieldName3] = $tester3->getPlainTextRepresentation($ezcoa3);
     // required to allow another call to metaData()
     // on $ezcoa3 in getPlainTextRepresentation, called from the getData() method :
     eZContentObject::recursionProtectionEnd();
     $fieldNameImageName3 = ezfSolrDocumentFieldBase::generateSubattributeFieldName($ezcca3, $imageName3->attribute('contentclass_attribute_identifier'), ezfSolrDocumentFieldObjectRelation::getClassAttributeType($imageName3->attribute('contentclass_attribute')));
     $expectedData3[$fieldNameImageName3] = trim(implode(' ', array_values(ezfSolrDocumentFieldBase::getInstance($imageName3)->getData())), "\t\r\n ");
     $fieldNameImageCaption3 = ezfSolrDocumentFieldBase::generateSubattributeFieldName($ezcca3, $imageCaption3->attribute('contentclass_attribute_identifier'), ezfSolrDocumentFieldObjectRelation::getClassAttributeType($imageCaption3->attribute('contentclass_attribute')));
     $expectedData3[$fieldNameImageCaption3] = trim(implode(' ', array_values(ezfSolrDocumentFieldBase::getInstance($imageCaption3)->getData())), "\t\r\n ");
     $image3 = eZContentObject::fetch($imageId3);
     $metaAttributeValues = eZSolr::getMetaAttributesForObject($image3);
     foreach ($metaAttributeValues as $metaInfo) {
         $expectedData3[ezfSolrDocumentFieldBase::generateSubmetaFieldName($metaInfo['name'], $ezcca3)] = ezfSolrDocumentFieldBase::preProcessValue($metaInfo['value'], $metaInfo['fieldType']);
     }
     $providerArray[] = array($expectedData3, $ezcoa3);
     #end 3
     // Let's perform the actual testing :
     foreach ($providerArray as $input) {
         $expected = $input[0];
         $contentObjectAttribute = $input[1];
         $instance = ezfSolrDocumentFieldBase::getInstance($contentObjectAttribute);
         self::assertEquals($expected, $instance->getData());
     }
 }
    /**
     * Creates an article in eZ Publish in the folder $folderId,
     * with title $articleTitle and summary $articleIntro and returns its ID.
     *
     * @param int $folderId
     * @param string $articleName
     * @param string $articleIntro
     * @return int The ID of the article created
     */
    public function createEZPArticle( $folderId, $articleTitle, $articleIntro, $articleRemoteID )
    {
        $object = new ezpObject( 'article', $folderId );
        $object->title = $articleTitle;
        $object->intro = $articleIntro;
        $object->publish();

        // Update object RemoteID
        $object->setAttribute( 'remote_id', $articleRemoteID );
        $object->store();

        $objectId = (int)$object->attribute( 'id' );
        return $objectId;
    }