<commit_before><?php
namespace Bolt\Tests\Storage;

use Bolt\Legacy\Storage;
use Bolt\Tests\BoltUnitTest;
use Bolt\Tests\Mocks\LoripsumMock;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test src/Storage/Repository and field transforms for load and hydrate
 *
 * @author <NAME> <<EMAIL>>
 */
class FieldLoadTest extends BoltUnitTest
{
    public function testRelationsLoad()
    {
        $this->resetDb();
        $app = $this->getApp();
        $app['schema']->update();
        $this->addDefaultUser($app);
        $this->addSomeContent();
        $em = $app['storage'];
        $repo = $em->getRepository('showcases');

        $record = $repo->find(1);

        foreach ($record->relation['entries'] as $entry) {
            $this->assertNotEmpty($entry->id);
            $this->assertNotEmpty($entry->slug);
        }
    }

    public function testTaxonomyLoad()
    {
        $app = $this->getApp();
        $em = $app['storage'];
        $repo = $em->getRepository('showcases');

        $record = $repo->find(1);
        $this->assertTrue(is_array($record->taxonomy['categories']));
        $this->assertTrue(is_array($record->taxonomy['tags']));
    }

    public function testRepeaterLoad()
    {
        $app = $this->getApp();
        $em = $app['storage'];
        $this->addSomeFields();
        $repo = $em->getRepository('showcases');
        $record = $repo->find(1);
        $this->assertInstanceOf('Bolt\Storage\Field\Collection\RepeatingFieldCollection', $record->repeater);
        $this->assertEquals(2, count($record->repeater));
        foreach ($record->repeater as $collection) {
            $this->assertInstanceOf('Bolt\Storage\Field\Collection\FieldCollection', $collection);
            foreach ($collection as $fieldValue) {
                $this->assertInstanceOf('Bolt\Storage\Entity\FieldValue', $fieldValue);
            }
        }
    }

    protected function addSomeContent()
    {
        $app = $this->getApp();
        $app['request'] = Request::create('/');
        $app['config']->set('taxonomy/categories/options', ['news']);
        $prefillMock = new LoripsumMock();
        $app['prefill'] = $prefillMock;

        $storage = new Storage($app);
        $storage->prefill(['showcases', 'entries', 'pages']);

        // We also set some relations between showcases and entries
        $showcases = $storage->getContent('showcases');
        $randEntries = $storage->getContent('entries/random/2');
        foreach ($showcases as $show) {
            foreach (array_keys($randEntries) as $key) {
                $show->setRelation('entries', $key);
                $storage->saveContent($show);
            }
        }
    }

    protected function addSomeFields()
    {
        $app = $this->getApp();
        $repo = $app['storage']->getRepository('showcases');
        $content = $repo->find(1);
        $repeat = [
            ['repeattitle' => 'Test', 'repeatimage' => ['file' => 'example.jpg', 'title' => 'Test Image']],
            ['repeattitle' => 'Test 2', 'repeatimage' => ['file' => 'example2.jpg', 'title' => 'Test Image 2']],
        ];
        $content->setRepeater($repeat);<commit_msg>add new tests for grouping taxonomies<commit_after><?php
namespace Bolt\Tests\Storage;

use Bolt\Legacy\Storage;
use Bolt\Tests\BoltUnitTest;
use Bolt\Tests\Mocks\LoripsumMock;
use Symfony\Component\HttpFoundation\Request;

/**
 * Class to test src/Storage/Repository and field transforms for load and hydrate
 *
 * @author <NAME> <<EMAIL>>
 */
class FieldLoadTest extends BoltUnitTest
{
    public function testRelationsLoad()
    {
        $this->resetDb();
        $app = $this->getApp();
        $app['schema']->update();
        $this->addDefaultUser($app);
        $this->addSomeContent();
        $em = $app['storage'];
        $repo = $em->getRepository('showcases');

        $record = $repo->find(1);

        foreach ($record->relation['entries'] as $entry) {
            $this->assertNotEmpty($entry->id);
            $this->assertNotEmpty($entry->slug);
        }
    }

    public function testTaxonomyLoad()
    {
        $app = $this->getApp();
        $em = $app['storage'];
        $repo = $em->getRepository('showcases');

        $record = $repo->find(1);
        $this->assertInstanceOf('Bolt\Storage\Collection\Taxonomy', $record->taxonomy['categories']);
        $this->assertInstanceOf('Bolt\Storage\Collection\Taxonomy', $record->taxonomy['tags']);
    }

    public function testRepeaterLoad()
    {
        $app = $this->getApp();
        $em = $app['storage'];
        $this->addSomeFields();
        $repo = $em->getRepository('showcases');
        $record = $repo->find(1);
        $this->assertInstanceOf('Bolt\Storage\Field\Collection\RepeatingFieldCollection', $record->repeater);
        $this->assertEquals(2, count($record->repeater));
        foreach ($record->repeater as $collection) {
            $this->assertInstanceOf('Bolt\Storage\Field\Collection\FieldCollection', $collection);
            foreach ($collection as $fieldValue) {
                $this->assertInstanceOf('Bolt\Storage\Entity\FieldValue', $fieldValue);
            }
        }
    }

    public function testGroupingTaxonomy()
    {
        $app = $this->getApp();
        $em = $app['storage'];
        $repo = $em->getRepository('pages');
        $record = $repo->find(3);
        $tax = $em->createCollection('Bolt\Storage\Entity\Taxonomy');
        $tax->setFromPost(['chapters'=>['main']], $record);
        $record->setTaxonomy($tax);
        $repo->save($record);
        $recordSaved = $repo->find(3);
        $this->assertInstanceOf('Bolt\Storage\Collection\Taxonomy', $record->taxonomy['chapters']);

    }

    protected function addSomeContent()
    {
        $app = $this->getApp();
        $app['request'] = Request::create('/');
        $app['config']->set('taxonomy/categories/options', ['news']);
        $prefillMock = new LoripsumMock();
        $app['prefill'] = $prefillMock;

        $storage = new Storage($app);
        $storage->prefill(['showcases', 'entries', 'pages']);

        // We also set some relations between showcases and entries
        $showcases = $storage->getContent('showcases');
        $randEntries = $storage->getContent('entries/random/2');
        foreach ($showcases as $show) {
            foreach (array_keys($randEntries) as $key) {
                $show->setRelation('entries', $key);
                $storage->saveContent($show);
            }
        }
    }

    protected function addSomeFields()
    {
        $app = $this->getApp();
        $repo = $app['storage']->getRepository('showcases');
        $content = $repo->find(1);

        $repeat = [
            ['repeattitle' => 'Test', 'repeatimage' => ['file' => 'example.jpg', 'title' => 'Test Image']],
            ['repeattitle' => 'Test 2', 'repeatimage' => ['file' => 'example2.jpg', 'title' => 'Test Image 2']],
        ];
        $content->setRepeater($repeat);
{'fraction_non_alphanumeric': 0.17737530544775046, 'fraction_numerical': 0.002874802357337933, 'mean_word_length': 2.6737064413938754, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 118, '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': '4929769', 'n_tokens_mistral': 2062, 'n_tokens_neox': 2029, 'n_words': 355}