<?php

namespace KupShop\ElninoBundle\Tests;

class UsersExportTest extends \DatabaseTestCase
{
    public static function setUpBeforeClass(): void
    {
        if (!findModule('elnino')) {
            self::markTestSkipped('Modul elnino je vypnutý.');
        }
    }

    public function testExportRequest()
    {
        $client = $this->createClient();

        $filters = [
            ['tag' => 'post_code', 'params' => [23202, 34]],
            ['tag' => 'sum_orders_price_lower', 'params' => ['100', '2014-08-19']],
            ['tag' => 'sum_orders_price_higher', 'params' => ['50', '2018-08-20']],
            ['tag' => 'sum_orders_price', 'params' => ['2015-08-19']],
            ['tag' => 'sum_orders', 'params' => ['2015-08-20']],
            ['tag' => 'last_order'],
            ['tag' => 'delivery_office'],
        ];

        $client->request('POST', '/uzivatele/export/', [], [], ['PHP_AUTH_USER' => 'demo', 'PHP_AUTH_PW' => 'demo'], json_encode($filters));

        $responseUrl = $client->getResponse()->getContent();

        $this->assertContains('pages_current_page":1,"pages_total_count":1', $responseUrl);
        $this->assertContains('["salutation_firstname","Luk\u00e1\u0161i"]', $responseUrl);
        $this->assertContains('["salutation_lastname","Kl\u00edmo"]', $responseUrl);
        $this->assertContains('["sum_orders",2],["sum_orders_price_higher",0]', $responseUrl);
        $this->assertContains('["sum_orders_price_lower",1]', $responseUrl);
        $this->assertContains('["sum_orders_price",600],["delivery_office",1]', $responseUrl);
        $this->assertContains('["sum_orders_price",708],["delivery_office",0]', $responseUrl);
        $this->assertContains('["last_order","2016-08-25 14:42:59"],["post_code",1]', $responseUrl);
    }

    public function testEmailUnregister()
    {
        $client = $this->createClient();
        $client->request('GET', '/uzivatele/disable/?email=hofferp@seznam.cz&key=d60db28d94d538bbb249dcc7f2273ab1');

        $user = $this->getQueryBuilder()->select('get_news')->from('users')
            ->where('email=:email')->setParameter('email', 'hofferp@seznam.cz')
            ->execute()->fetchColumn();

        $this->assertEquals('N', $user);
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
