<commit_before><?php

namespace PHPOrchestra\ApiBundle\FunctionalTest\Controller;

/**
 * Class NodeControllerTest
 */
class NodeControllerTest extends AbstractControllerTest
{
    public function testDeleteAction()
    {
        $this->prepareDatabase();
        $crawler = $this->client->request('GET', '/admin/');
        $nbLink = $crawler->filter('a')->count();

        $crawler = $this->client->request('DELETE', '/api/node/fixture_deleted/delete');

        $crawler = $this->client->request('GET', '/admin/');

        $this->assertCount($nbLink - 2, $crawler->filter('a'));
    }

    /**
     * @param $nodes
     */
    protected function undeleteNodes($nodes)
    {
        foreach ($nodes as $node) {
            $node->setDeleted(false);
        }
    }

    /**
     * @return array
     */
    protected function prepareDatabase()
    {
        $nodes = $this->nodeRepository->findByNodeIdAndSiteId('fixture_deleted');
        $this->undeleteNodes($nodes);
        $sons = $this->nodeRepository->findByParentIdAndSiteId('fixture_deleted_son');
        $this->undeleteNodes($sons);

        static::$kernel->getContainer()->get('doctrine.odm.mongodb.document_manager')->flush();
    }
}
<commit_msg>add functional test for node duplication and creation of new language node<commit_after><?php

namespace PHPOrchestra\ApiBundle\FunctionalTest\Controller;
use PHPOrchestra\ModelBundle\Model\NodeInterface;

/**
 * Class NodeControllerTest
 */
class NodeControllerTest extends AbstractControllerTest
{
    public function testDeleteAction()
    {
        $this->prepareDatabase();
        $crawler = $this->client->request('GET', '/admin/');
        $nbLink = $crawler->filter('a')->count();

        $crawler = $this->client->request('DELETE', '/api/node/fixture_deleted/delete');

        $crawler = $this->client->request('GET', '/admin/');

        $this->assertCount($nbLink - 2, $crawler->filter('a'));
    }

    /**
     * @param $nodes
     */
    protected function undeleteNodes($nodes)
    {
        foreach ($nodes as $node) {
            $node->setDeleted(false);
        }
    }

    /**
     * @return array
     */
    protected function prepareDatabase()
    {
        $nodes = $this->nodeRepository->findByNodeIdAndSiteId('fixture_deleted');
        $this->undeleteNodes($nodes);
        $sons = $this->nodeRepository->findByParentIdAndSiteId('fixture_deleted_son');
        $this->undeleteNodes($sons);

        static::$kernel->getContainer()->get('doctrine.odm.mongodb.document_manager')->flush();
    }

    /**
     * Test node duplicate and references
     */
    public function testDuplicateNode()
    {
        $node = $this->nodeRepository
            ->findOneByNodeIdAndLanguageAndSiteIdAndLastVersion('fixture_full', 'fr', '1');
        $nodeTransverse = $this->nodeRepository
            ->findOneBy(array('nodeId' => NodeInterface::TRANSVERSE_NODE_ID, 'language' => 'fr', 'siteId' => '1'));

        $crawler = $this->client->request('POST', '/api/node/fixture_full/duplicate?language=fr');

        $nodeLastVersion = $this->nodeRepository
            ->findOneByNodeIdAndLanguageAndSiteIdAndLastVersion('fixture_full', 'fr', '1');

        $nodeRepository = static::$kernel->getContainer()->get('php_orchestra_model.repository.node');
        $nodeTransverseAfter = $nodeRepository
            ->findOneBy(array('nodeId' => NodeInterface::TRANSVERSE_NODE_ID, 'language' => 'fr', 'siteId' => '1'));

        $this->assertSame($node->getVersion()+1, $nodeLastVersion->getVersion());
        $this->assertGreaterThan($this->countAreaRef($nodeTransverse), $this->countAreaRef($nodeTransverseAfter));
    }

    /**
     * Test creation of new language for a node
     */
    public function testCreateNewLanguageNode()
    {
        $node = $this->nodeRepository
            ->findOneByNodeIdAndLanguageAndSiteIdAndLastVersion('fixture_full', 'en', '1');

        $nodeTransverse = $this->nodeRepository
            ->findOneBy(array('nodeId' => NodeInterface::TRANSVERSE_NODE_ID, 'language' => 'en', 'siteId' => '1'));
        $countAreaRef = $this->countAreaRef($nodeTransverse);

        $this->assertSame(null, $node);
        $this->assertSame(1, $countAreaRef);

        $crawler = $this->client->request('GET', '/api/node/fixture_full', array('language' => 'en'));


        $nodeRepository = static::$kernel->getContainer()->get('php_orchestra_model.repository.node');
        $nodeTransverseAfter = $nodeRepository
            ->findOneBy(array('nodeId' => NodeInterface::TRANSVERSE_NODE_ID, 'language' => 'en', 'siteId' => '1'));

        $this->assertGreaterThan($countAreaRef, $this->countAreaRef($nodeTransverseAfter));
    }

    /**
     * @param NodeInterface $node
     *
     * @return int
     */
    public function countAreaRef(NodeInterface $node)
    {
        $areaRef = 0;
        foreach ($node->getBlocks() as $block) {
            $areaRef = $areaRef + count($block->getAreas());
        }

        return $areaRef;
    }
}

{'fraction_non_alphanumeric': 0.16309785871522914, 'fraction_numerical': 0.0024014408645187113, 'mean_word_length': 3.1511627906976742, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3831789', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1480, 'n_words': 221}