Untitled

 avatar
user_4191664
plain_text
2 years ago
74 kB
2
Indexable
<?php

namespace CodeIgniter;

use CodeIgniter\Router\Exceptions\RedirectException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\FeatureTestTrait;
use Tests\Support\Database\Seeds\Beneficiari;
use Tests\Support\Database\Seeds\IpUserMeta;
use Tests\Support\Database\Seeds\IpUsers;
use Tests\Support\Database\Seeds\MainCategories;
use Tests\Support\Database\Seeds\Permissions;
use Tests\Support\Database\Seeds\RapoarteTemplate;
use Tests\Support\Database\Seeds\Sources;
use Tests\Support\Database\Seeds\TipAbonament;

class RapoarteTest extends CIUnitTestCase
{
    use DatabaseTestTrait;
    use FeatureTestTrait;

    protected $setUpMethods = [
        'resetServices',
        'mockSession',
    ];
    protected $migrateOnce = true;

    protected $seedOnce = true;

    protected $namespace = ['Tests\Support'];

    protected $seed = [
        Sources::class,
        MainCategories::class,
        TipAbonament::class,
        IpUsers::class,
        IpUserMeta::class,
        Beneficiari::class,
        Permissions::class,
        RapoarteTemplate::class,
    ];

    private $userSessionCompanie = [
        'user_id' => '1',
        'user_role' => 'companie',
        'id_companie' => '1',
        'logged_in' => 1,
        'tip' => 'subscription',
        'afisare' => []
    ];

    protected $companyUserId = 2; //Seeders



    protected $nameRaport = 'Raport test';


    public function testRapoartePageIndex()
    {
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte');
        $result->assertStatus(200);

        $result->assertSee('LISTA RAPOARTE');

        $result->assertSee('Adauga template');
        //$result->assertDontSee('Raport cuvinte de specialitate');

    }

    public function testAddRaportPage()
    {
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte/adauga_raport_template');
        $result->assertStatus(200);

        $result->assertSee('ADAUGA RAPORT');
        $result->assertSee('Denumire Raport');
        $result->assertSee('Client 1');
        $result->assertSee('Client 2');
    }

    public function testAddSuccessfullyRaportTemplate()
    {
        $clientId = 10; //Seeders

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/ajax_raport_keys', [
                'post' => [
                    'name_raport' => $this->nameRaport,
                    'description_raport' => 'Raport test description added by test',
                ],
                'clienti_raport' => [
                    $clientId
                ],
                'subcategories' => [
                    'Romania', 'Europa'
                ],

                'json' => [
                    [
                        'name' => 'Romania',
                        'score' => 10,
                        'type' => 'parent_keys',
                        'id' => 1,
                        'children' => [
                            [
                                'name' => 'legea',
                                'score' => 6,
                                'type' => 'child_keys',
                                'id' => 2,
                            ],
                            [
                                'name' => 'legi',
                                'score' => 7,
                                'type' => 'child_keys',
                                'id' => 4,
                            ],
                            [
                                'name' => 'Europa',
                                'score' => 10,
                                'type' => 'exclude_keys',
                                'id' => 5,
                            ]
                        ]
                    ],

                ]
            ]);
        $result->assertStatus(200);

        // Check id key combination
        $this->seeInDatabase('key_combinations', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'status' => 1,
        ]);
        $keyCombinationId = $this->db->table('key_combinations')->select('id')
            ->where('id_user', $this->userSessionCompanie['user_id'])
            ->where('id_companie', $this->userSessionCompanie['id_companie'])
            ->where('status', 1)
            ->orderBy('id', 'desc')->get()->getRowArray()['id'];

        // Check rapoarte table
        $this->seeInDatabase('ip_rapoarte', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
            'description_raport' => 'Raport test description added by test',
            'categories' => 'Romania<>Europa',
            'id_key' => $keyCombinationId,
        ]);

        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
            'description_raport' => 'Raport test description added by test',
            'categories' => 'Romania<>Europa',
            'id_key' => $keyCombinationId,
        ]);



        //Check rapoarte logs
        $this->seeInDatabase('ip_rapoarte_log', [
            'id_raport' => $raportId,
            'action' => 'a creat',
            'target' => 'raportul template',
            'modified_by' => $this->userSessionCompanie['user_id'],
        ]);

        // Check key parent
        $this->seeInDatabase('key_parent', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
            'key_score' => 10,
        ]);

        // Check key child
        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
            'key_score' => 6,
        ]);

        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
            'key_score' => 7,
        ]);

        // Check key exclude
        $this->seeInDatabase('key_exclude', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
            'key_score' => 10,
        ]);

        // get id keys
        $idKeyParent = $this->grabFromDatabase('key_parent', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
            'key_score' => 10,
        ]);

        $idKeyChild1 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
            'key_score' => 6,
        ]);

        $idKeyChild2 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
            'key_score' => 7,
        ]);

        $idKeyExclude = $this->grabFromDatabase('key_exclude', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
            'key_score' => 10,
        ]);

        // Check association keys
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild1,
        ]);
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild2,
        ]);
        $this->seeInDatabase('key_associations_exclude', [
            'key_parent' => $idKeyParent,
            'key_exclude' => $idKeyExclude,
        ]);

        // Check client_raport
        $this->seeInDatabase('client_raport', [
            'id_raport' => $raportId,
            'id_client' => $clientId,
        ]);


    }

    public function testPageRapoarteWithRaport()
    {
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte');
        $result->assertStatus(200);
        $result->assertSee('LISTA RAPOARTE');
        $result->assertSee($this->nameRaport);
    }

    public function testOpenPermisions()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/permisiuni_edit_raport',[
                'raport_id' => $raportId,
            ]);

        $result->assertStatus(200);
        $result->assertSee('PERMISIUNI');
        $result->assertSee($this->nameRaport);
        $result->assertDontSee('John Smith');
        $result->assertSee('John 2 Smith 2');
        $result->assertSee('Salveaza permisiuni');

        $json = json_decode($result->getJSON(), true);
        $html = $json['html'];
        $this->assertStringContainsString('John 2 Smith 2', $html);
        $this->assertStringNotContainsString('John Smith', $html);
        $this->assertStringNotContainsString('John 3 Smith 3', $html);
        // Check if value is 0 as in has no permission
        $this->assertStringContainsString('class="js-pseudo-value-inp" name="' . $this->companyUserId . '" value="0"', $html);
    }

    public function testFailedOpenPermissions()
    {
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/permisiuni_edit_raport',[
                'raport_id2' => 1,
            ]);
        $result->assertStatus(200);
        $result->assertSee('Error raport');

        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/permisiuni_edit_raport',[
                'raport_id' => 'ab',
            ]);
        $result->assertStatus(200);
        $result->assertSee('Error raport numeric');
    }

    public function testSavePermisiuni()
    {
        // path: rapoarte/permisiuni_save_raport/2
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $permisiuni = $this->db->table('permissions')->whereIn('permission', ['read_raport', 'edit_raport'])->get()->getResult();
        $readId = 0;
        $editId = 0;
        foreach ($permisiuni as $permisiune) {
            if ($permisiune->permission == 'read_raport') {
                $readId = $permisiune->id;
            }
            if ($permisiune->permission == 'edit_raport') {
                $editId = $permisiune->id;
            }
        }

        // Change to read
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/permisiuni_save_raport/' . $raportId,[
                $this->companyUserId => $readId
            ]);

        $result->assertStatus(200);
        $result->assertSee('"status": "saved"');

        // Change to edit
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/permisiuni_save_raport/' . $raportId,[
                $this->companyUserId => $editId
            ]);

        $result->assertStatus(200);
        $result->assertSee('"status": "saved"');

        // Check if saved
        $this->seeInDatabase('rapoarte_permissions', [
            'id_raport' => $raportId,
            'id_user' => $this->companyUserId,
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'id_permissions' => $editId,
        ]);
    }

    public function testEditareRaportTemplatePage()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte/editare_raport_template/' . $raportId,);
        $result->assertStatus(200);
        $result->assertSee('EDITARE RAPORT');
        $result->assertSee( $this->nameRaport);
        $result->assertSee('Romania');
        $result->assertSee('Europa');
        $result->assertSee('Client 1');
        $result->assertSee('Client 2');
        $clientId = 10;

        $result->assertSee('<input type="checkbox" checked hidden="hidden" name="clienti_raport[]" value="'.$clientId.'"');
        $result->assertSee('<div class="sub-category-raport">Romania');

    }

    public function testUpdateDescriptionOnlyRapoarteTemplate()
    {
        // path: /rapoarte/update_ajax_raport_keys/2
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $clientId = 10;
        $clientId2 = 11;

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/update_ajax_raport_keys/' . $raportId,[
                'post' => [
                    'name_raport' => $this->nameRaport,
                    'description_raport' => 'Raport test description added by test and modified',
                ],
                'clienti_raport' => [
                    $clientId
                ],
                'subcategories' => [
                    'Romania', 'Europa'
                ],

                'json' => [
                    [
                        'name' => 'Romania',
                        'score' => 10,
                        'type' => 'parent_keys',
                        'id' => 1,
                        'children' => [
                            [
                                'name' => 'legea',
                                'score' => 8, // changed
                                'type' => 'child_keys',
                                'id' => 2,
                            ],
                            [
                                'name' => 'legi',
                                'score' => 8, // changed
                                'type' => 'child_keys',
                                'id' => 4,
                            ],
                            [
                                'name' => 'Europa',
                                'score' => 8, // changed
                                'type' => 'exclude_keys',
                                'id' => 5,
                            ],
                            // new keys
                            [
                                'name' => 'World',
                                'score' => 8,
                                'type' => 'exclude_keys',
                                'id' => 5,
                            ]
                        ]
                    ],

                ],
                'modificat_keys' => 0,
            ]);

        $result->assertStatus(200);
        $result->assertSee('1');

        // Check if saved
        $this->seeInDatabase('ip_rapoarte', [
            'id' => $raportId,
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
            'description_raport' => 'Raport test description added by test and modified',
        ]);

        // Check log
        $this->seeInDatabase('ip_rapoarte_log', [
            'id_raport' => $raportId,
            'action' => 'a modificat',
            'target' => 'raportul template',
            'modified_by' => $this->userSessionCompanie['user_id'],
        ]);

        $keyCombinationId = $this->db->table('ip_rapoarte')->where('id', $raportId)->get()->getRow()->id_key;

        $this->seeInDatabase('key_parent', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
            'key_score' => 10,
        ]);
        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
            'key_score' => 6,
        ]);
        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
            'key_score' => 7,
        ]);

        // Check key exclude
        $this->seeInDatabase('key_exclude', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
            'key_score' => 10,
        ]);
        $this->dontSeeInDatabase('key_exclude', [
            'id_key' => $keyCombinationId,
            'key_word' => 'World',
            'key_score' => 10,
        ]);


        $idKeyParent = $this->grabFromDatabase('key_parent', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
            'key_score' => 10,
        ]);

        $idKeyChild1 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
            'key_score' => 6,
        ]);

        $idKeyChild2 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
            'key_score' => 7,
        ]);

        $idKeyExclude = $this->grabFromDatabase('key_exclude', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
            'key_score' => 10,
        ]);

        // Check association keys
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild1,
        ]);
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild2,
        ]);
        $this->seeInDatabase('key_associations_exclude', [
            'key_parent' => $idKeyParent,
            'key_exclude' => $idKeyExclude,
        ]);

        // Check client_raport
        $this->seeInDatabase('client_raport', [
            'id_raport' => $raportId,
            'id_client' => $clientId,
        ]);

        $this->dontSeeInDatabase('client_raport', [
            'id_raport' => $raportId,
            'id_client' => $clientId2,
        ]);
    }

    public function testUpdateRaportTemplate()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $clientId = 10;
        $clientId2 = 11;

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/rapoarte/update_ajax_raport_keys/' . $raportId,[
                'post' => [
                    'name_raport' => $this->nameRaport,
                    'description_raport' => 'Raport test description added by test and modified again',
                ],
                'clienti_raport' => [
                    $clientId, $clientId2
                ],
                'subcategories' => [
                    'Romania', 'Europa', 'World'
                ],

                'json' => [
                    [
                        'name' => 'Romania',
                        'score' => 9, // changed
                        'type' => 'parent_keys',
                        'id' => 1,
                        'children' => [
                            [
                                'name' => 'legea',
                                'score' => 8, // changed
                                'type' => 'child_keys',
                                'id' => 2,
                            ],
                            [
                                'name' => 'legi',
                                'score' => 8, // changed
                                'type' => 'child_keys',
                                'id' => 4,
                            ],
                            [
                                'name' => 'Europa',
                                'score' => 8, // changed
                                'type' => 'exclude_keys',
                                'id' => 5,
                            ],
                            // new keys
                            [
                                'name' => 'World',
                                'score' => 8,
                                'type' => 'exclude_keys',
                                'id' => 5,
                            ]
                        ]
                    ],

                ],
                'modificat_keys' => 1,
            ]);

        $result->assertStatus(200);
        $result->assertSee('1');

        $this->seeInDatabase('ip_rapoarte', [
            'id' => $raportId,
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
            'description_raport' => 'Raport test description added by test and modified again',
            'categories' => 'Romania<>Europa<>World',
        ]);

        $modifiedTimes = $this->db->table('ip_rapoarte_log')->where('id_raport', $raportId)
            ->where('action', 'a modificat')->countAllResults();
        $this->assertEquals(2, $modifiedTimes);

        $keyCombinationId = $this->db->table('ip_rapoarte')->where('id', $raportId)->get()->getRow()->id_key;
        $this->seeInDatabase('key_parent', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
            'key_score' => 9,
        ]);


        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
            'key_score' => 8,
        ]);
        $this->seeInDatabase('key_child', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
            'key_score' => 8,
        ]);

        // Check key exclude
        $this->seeInDatabase('key_exclude', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
            'key_score' => 8,
        ]);
        $this->seeInDatabase('key_exclude', [
            'id_key' => $keyCombinationId,
            'key_word' => 'World',
            'key_score' => 8,
        ]);


        $idKeyParent = $this->grabFromDatabase('key_parent', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Romania',
        ]);

        $idKeyChild1 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legea',
        ]);

        $idKeyChild2 = $this->grabFromDatabase('key_child', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'legi',
        ]);

        $idKeyExclude = $this->grabFromDatabase('key_exclude', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'Europa',
        ]);
        $idKeyExclude2 = $this->grabFromDatabase('key_exclude', 'id', [
            'id_key' => $keyCombinationId,
            'key_word' => 'World',
        ]);
        // Check association keys
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild1,
        ]);
        $this->seeInDatabase('key_associations_child', [
            'key_parent' => $idKeyParent,
            'key_child' => $idKeyChild2,
        ]);
        $this->seeInDatabase('key_associations_exclude', [
            'key_parent' => $idKeyParent,
            'key_exclude' => $idKeyExclude,
        ]);
        $this->seeInDatabase('key_associations_exclude', [
            'key_parent' => $idKeyParent,
            'key_exclude' => $idKeyExclude2,
        ]);

        // Check client_raport
        $this->seeInDatabase('client_raport', [
            'id_raport' => $raportId,
            'id_client' => $clientId,
        ]);

        $this->seeInDatabase('client_raport', [
            'id_raport' => $raportId,
            'id_client' => $clientId2,
        ]);
    }

    public function testAddRaportSinglePage()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', 'rapoarte/raport_nou/' . $raportId);

        $result->assertStatus(200);
        $result->assertSee('CREEAZA RAPORT');
        $result->assertSee('Nume raport');

    }

    public function testAjaxValidationCreateRaportSingle()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', 'rapoarte/ajax_polling', [
                'raport' => $raportId,

            ]);
        $result->assertSee('Nu au fost introduse toate campurile');
    }

    public function testAjaxValidationDatesCreateRaportSingle()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', 'rapoarte/ajax_polling', [
                'raport' => $raportId,
                'name_raport' => 'test',
                'start_date' => '2020-201-201',
                'end_date' => '2020-201-201',
            ]);
        $result->assertSee('Invalid dates');
    }



    public function testSuccessCreateRaportSingle()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $keyId = $this->grabFromDatabase('ip_rapoarte', 'id_key', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', 'rapoarte/ajax_polling', [
                'raport' => $raportId,
                'name_raport' => 'Test 106 articles',
                'start_date' => '2023-05-01 00:00:00',
                'end_date' => '2023-05-02 00:00:00',
            ]);

        // Total articles for these should be 106 articles

        $this->seeInDatabase('ip_rapoarte_single',[
            'id_raport' => $raportId,
            'name_raport' => 'Test 106 articles',
            'date_start' => '2023-05-01 00:00:00',
            'date_end' => '2023-05-02 00:00:00',
            'total_articole' => 106,
            'modified_articole' => 0,
            'current_id_key' => $keyId,
        ]);

    }

    public function testArticleListPage()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);

        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);


        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte/editare_raport/' . $raportSingleId);

        $result->assertSee('LISTA ARTICOLE');
        $result->assertSee('<span class="c--secondary js-total-articole"> 106</span>');

    }

    public function testGetArticleListRaportZilnic()
    {

        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId , [
                'raport' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(106, $json['recordsTotal']);
        $this->assertStringContainsString('Ce salariu primeşte un muncitor din Sri Lanka, în România: „Sunt plătiti mai bine decât românii pentru că muncesc mai mult”',
            $json['data'][0]['titlu_articol']);
    }


    /**
     * Test open filter modal on raport zilnic (newly created)
     * @return void
     * @throws RedirectException
     */
    public function testArticoleFiltreza()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/filtreaza' , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertStringContainsString('Filtreaza', $json['html']);
        $this->assertStringContainsString('Tone of voice', $json['html']);

        // Assert no filter exists in database
        $this->dontSeeInDatabase('ip_rapoarte_single_filter',[
            'id_raport_single' => $raportSingleId,

        ]);
    }

    /**
     * Test filter works after creating a raport zilnic(newly created and no modifications)
     *
     * @return void
     * @throws RedirectException
     */
    public function testSaveArticoleFiltreaza()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', 'articole/save_filter_options', [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'status_editat' => 'Editat',
                'scor_min' => '',
                'scor_max' => '',
                'data_publicarii' => '',
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->assertEquals(0, $json['total_articole']);

        // Assert filter exists in database
        $this->seeInDatabase('ip_rapoarte_single_filter',[
            'id_raport_single' => $raportSingleId,
            'meta_key' => 'status_editat',
            'meta_value' => 1,
        ]);

        // Get all articles, and it should return 0 since no article has been editet yet
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId , [
                'raport' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(0, $json['recordsTotal']);

        // Change status_editat to 'Needitate' and you will get back all articles
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', 'articole/save_filter_options', [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'status_editat' => 'Needitat',
                'scor_min' => '',
                'scor_max' => '',
                'data_publicarii' => '',
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->assertEquals(106, $json['total_articole']);

        // Assert filter exists in database
        $this->seeInDatabase('ip_rapoarte_single_filter',[
            'id_raport_single' => $raportSingleId,
            'meta_key' => 'status_editat',
            'meta_value' => '0',
        ]);

        // Get all articles, and it should return 106 since no article has been edited yet
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId , [
                'raport' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(106, $json['recordsTotal']);



        // Change status_editat to 'Toate' and you will get back all articles
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', 'articole/save_filter_options', [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'status_editat' => 'Toate',
                'scor_min' => '',
                'scor_max' => '',
                'data_publicarii' => '',
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->assertEquals(106, $json['total_articole']);

        // Assert filter exists in database
        $this->seeInDatabase('ip_rapoarte_single_filter',[
            'id_raport_single' => $raportSingleId,
            'meta_key' => 'status_editat',
            'meta_value' => 'skip_query',
        ]);

        // Get all articles, and it should return 106 since all articles are returned
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId , [
                'raport' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(106, $json['recordsTotal']);
    }

    public function testEditeazaArticol()
    {

        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);
        $articolId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'id_raport' => $raportId,
            'id_raport_single' => $raportSingleId,
        ]);



        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/editeaza_articol'  , [
                'raport' => $raportId,
                'articol_salvat' => $articolId,
            ]);

        $result->assertStatus(200);
        $result->assertSee("Ce salariu primeşte un muncitor din Sri Lanka, în România:");
        $json = json_decode($result->getJSON(), true);
      /*  $result->assertSee('<p id="caracter_articol" class="pseudo-input">Neutru</p>');
        $result->assertSee('<p id="dedicat_articol" class="pseudo-input">Dedicat</p>');
        $result->assertSee('<p id="relevant_articol" class="pseudo-input">Relevant</p>');
        $result->assertSee('<p id="categorie_articol" class="pseudo-input">-</p>');
        $result->assertSee('<input class="main-inputs" type="text" id="autor_articol" name="autor_articol" value="Forcos George" placeholder="adauga autor">');
   */
        $this->assertStringContainsString('<p id="caracter_articol" class="pseudo-input">Neutru</p>', $json['results']);
        $this->assertStringContainsString('<p id="dedicat_articol" class="pseudo-input">Dedicat</p>', $json['results']);
        $this->assertStringContainsString('<p id="relevant_articol" class="pseudo-input">Relevant</p>', $json['results']);
        $this->assertStringContainsString('<p id="categorie_articol" class="pseudo-input">-</p>', $json['results']);
        $this->assertStringContainsString('<input class="main-inputs" type="text" id="autor_articol" name="autor_articol" value="Forcos George"', $json['results']);
    }

    public function testSaveArticol()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);
        $articolId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'id_raport' => $raportId,
            'id_raport_single' => $raportSingleId,
        ]);

        $article = $this->db->table('ip_articole_salvate')->where('id', $articolId)->get()->getRow();


        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolId,
                'title_article' => 'Editat - ' . $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => 'Pozitiv',
                'categorie_articol' => 'Romania',
                'dedicat_articol' => 'Mentionat',
                'relevant_articol' => 'Nerelevant',
                'autor_articol' => 'Editat- ' . $article->author,
                'publication_date' => '2023-05-20 13:12:31',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        // Now test if the article was saved

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/editeaza_articol'  , [
                'raport' => $raportId,
                'articol_salvat' => $articolId,
            ]);

        $result->assertStatus(200);
        $result->assertSee("Editat - Ce salariu primeşte un muncitor din Sri Lanka, în România:");
        $json = json_decode($result->getJSON(), true);
        $this->assertStringContainsString('<p id="caracter_articol" class="pseudo-input">Pozitiv</p>', $json['results']);
        $this->assertStringContainsString('<p id="dedicat_articol" class="pseudo-input">Mentionat</p>', $json['results']);
        $this->assertStringContainsString('<p id="relevant_articol" class="pseudo-input">Nerelevant</p>', $json['results']);
        $this->assertStringContainsString('<p id="categorie_articol" class="pseudo-input">Romania</p>', $json['results']);
        $this->assertStringContainsString('<input class="main-inputs" type="text" id="autor_articol" name="autor_articol" value="Editat- Forcos George"', $json['results']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolId,
            'status_editat' => 1,
        ]);
    }

    public function testStergeArticol()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);
        $article = $this->db->table('ip_articole_salvate')
            ->where('id_raport_single', $raportSingleId)
            ->orderBy('id', 'desc')
            ->get()->getRow();

        $articleId = $article->id;

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId, [
                'raport' => $raportSingleId,
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(106, $json['recordsTotal']);

        // Delete articol
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/sterge_articol', [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articleId,
            ]);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleId,
            'status' => 0,
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/articol/articol/' . $raportSingleId, [
                'raport' => $raportSingleId,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals(105, $json['recordsTotal']);


        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', 'articole/restore_articol', [
                'raport' => $raportId,
                'raport_single_id' => $raportSingleId,
                'articol_restore' => $articleId,
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleId,
            'status' => 1,
        ]);

    }

    public function testSetArticleAsPrincipal()
    {
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);
        $articles = $this->db->table('ip_articole_salvate')
            ->where('id_raport_single', $raportSingleId)
            ->orderBy('id', 'asc')
            ->limit(10)
            ->get()->getResult();

        $articleMainId = $articles[0]->id;
        $article = $this->db->table('ip_articole_salvate')->where('id', $articleMainId)->get()->getRow();



        $stiri_conectate = [
            $this->grabFromDatabase('ip_articole_salvate', 'id', [
                'title' => 'Ce se întâmplă cu Legea arendei? Nina Gheorghiță, LAPAR: Durata minimă de arendare e prima nevoie a agriculturii!',
            ]),
            $this->grabFromDatabase('ip_articole_salvate', 'id', [
                'title' => 'România, prima calificare la Europeanul de Handbal, cu mari emoții, după aproape 30 de ani / Istoria „tricolorilor” la EURO și la Mondiale',
            ]),
            $this->grabFromDatabase('ip_articole_salvate', 'id', [
                'title' => 'Țară mare, angajați puțini. România are cel mai mic procent de persoane active, raportat la media europeană',
            ]),
            $this->grabFromDatabase('ip_articole_salvate', 'id', [
                'title' => 'Survivor România 1 mai 2023. Plecări pe bandă rulantă, surpriză totală. Când se anunţă marele câştigător',
            ]),

        ];

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articleMainId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => 'Pozitiv',
                'categorie_articol' => 'Romania',
                'dedicat_articol' => 'Mentionat',
                'relevant_articol' => 'Nerelevant',
                'autor_articol' => $article->author,
                'publication_date' => '2023-05-20 13:12:31',
                'stiri_conectate' => $stiri_conectate,
                'tip_articol' => 'principal',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'title' => $article->title,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectate),
        ]);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $stiri_conectate[0],
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $stiri_conectate[1],
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $stiri_conectate[2],
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $stiri_conectate[3],
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
        ]);


        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/articole/compara_articole', [
                'raport' => $raportId,
                'articol_salvat' => $articleMainId,
                'raport_single' => $raportSingleId,
                'offset' => 0,
                'limit' => 10,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->assertEquals(4, $json['$results_count']);
        $this->assertEquals('Ce se întâmplă cu Legea arendei? Nina Gheorghiță, LAPAR: Durata minimă de arendare e prima nevoie a agriculturii!',
            $json['results'][0]['title']
        );
        $this->assertEquals('România, prima calificare la Europeanul de Handbal, cu mari emoții, după aproape 30 de ani / Istoria „tricolorilor” la EURO și la Mondiale',
            $json['results'][1]['title']
        );
        $this->assertEquals('Țară mare, angajați puțini. România are cel mai mic procent de persoane active, raportat la media europeană',
            $json['results'][2]['title']
        );
        $this->assertEquals('Survivor România 1 mai 2023. Plecări pe bandă rulantă, surpriză totală. Când se anunţă marele câştigător',
            $json['results'][3]['title']
        );

        // Next should be article 2 from the list
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/articole/compara_articole', [
                'raport' => $raportId,
                'articol_salvat' => $articleMainId,
                'raport_single' => $raportSingleId,
                'offset' => count($stiri_conectate),
                'limit' => 10,
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->assertEquals(4, $json['$results_count']);
        $this->assertEquals('„Avem o ciumă a pensiilor speciale care este o inechitate”. Ciolacu: România trebuie să facă anumite reforme asumate în PNRR',
            $json['results'][0]['title']
        );


        // Save articol asociat without modifying the main article
        $articolId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'Survivor România 1 mai 2023. Plecări pe bandă rulantă, surpriză totală. Când se anunţă marele câştigător',
        ]);
        $article = $this->db->table('ip_articole_salvate')->where('id', $articolId)->get()->getRow();
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => $article->caracter_articol,
                'categorie_articol' => $article->categorie,
                'dedicat_articol' => $article->dedicat,
                'relevant_articol' => $article->relevant,
                'autor_articol' => $article->author,
                'publication_date' => $article->publication_date,
                'stire_principala' => $article->stiri_conectate,
                'tip_articol' => 'asociat',
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolId,
            'title' => 'Survivor România 1 mai 2023. Plecări pe bandă rulantă, surpriză totală. Când se anunţă marele câştigător',
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
        ]);

        // #19 - Articol asociat (id=7) salvat ca articol principal , verificat si articolul principal (id=1)
        $articleMainIdNew = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => '„Avem o ciumă a pensiilor speciale care este o inechitate”. Ciolacu: România trebuie să facă anumite reforme asumate în PNRR',
        ]);
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => $article->caracter_articol,
                'categorie_articol' => $article->categorie,
                'dedicat_articol' => $article->dedicat,
                'relevant_articol' => $article->relevant,
                'autor_articol' => $article->author,
                'publication_date' => $article->publication_date,
                'stire_principala' => $article->stiri_conectate,
                'stiri_conectate' => [
                    $articleMainIdNew
                ],
                'tip_articol' => 'principal',
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        // check this new articol principal
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolId,
            'title' => 'Survivor România 1 mai 2023. Plecări pe bandă rulantă, surpriză totală. Când se anunţă marele câştigător',
            'stire_originala' => 'articol principal',
            'stiri_conectate' => $articleMainIdNew,
        ]);
        // check new articol asociat
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainIdNew,
            'title' => '„Avem o ciumă a pensiilor speciale care este o inechitate”. Ciolacu: România trebuie să facă anumite reforme asumate în PNRR',
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articolId,
        ]);
        // check old articol principal
        //Remove from array $stiri_conectate the id of the article that was saved as articol principal;
        $stiri_conectate = array_diff($stiri_conectate, [$articolId]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectate),
        ]);


        // #19 - Articol asociat (id=6) sters si verificat articolul principal(id=1)
        $articolStersId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'Țară mare, angajați puțini. România are cel mai mic procent de persoane active, raportat la media europeană',
        ]);
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/sterge_articol'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolStersId,
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        // Check if the article was deleted
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolStersId,
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
            'status' => 0,
        ]);
        // Check old articol principal has removed this article
        $stiri_conectate = array_diff($stiri_conectate, [$articolStersId]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectate),
        ]);




        // #19 - Articol asociat (id=4) sters si restore (id=1)
        $articolStersRestoreId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'România, prima calificare la Europeanul de Handbal, cu mari emoții, după aproape 30 de ani / Istoria „tricolorilor” la EURO și la Mondiale',
        ]);
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/sterge_articol'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolStersRestoreId,
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        // Check if the article was deleted
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolStersRestoreId,
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainId,
            'status' => 0,
        ]);
        $stiri_conectateTemp = array_diff($stiri_conectate, [$articolStersRestoreId]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectateTemp),
        ]);

        // Restore the article
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', 'articole/restore_articol', [
                'raport' => $raportId,
                'raport_single_id' => $raportSingleId,
                'articol_restore' => $articolStersRestoreId,
            ]);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolStersRestoreId,
            'status' => 1,
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectate),
        ]);

        // #19 Articol asociat (id=3) cu save ca articol asociat pt alt principal si verificat ambele articole principale
        $articolAsociatId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'România, prima calificare la Europeanul de Handbal, cu mari emoții, după aproape 30 de ani / Istoria „tricolorilor” la EURO și la Mondiale',
        ]);
        $article = $this->db->table('ip_articole_salvate')->where('id', $articolAsociatId)->get()->getRow();
        $articleMainIdNew2 = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'România nu reușește să atragă banii din PNRR. Gradul de absorbție este de 0,3%, banii zac nefolosiţi în Trezorerie',
        ]);
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articolAsociatId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => $article->caracter_articol,
                'categorie_articol' => $article->categorie,
                'dedicat_articol' => $article->dedicat,
                'relevant_articol' => $article->relevant,
                'autor_articol' => $article->author,
                'publication_date' => $article->publication_date,
                'stire_principala' => $articleMainIdNew2,
                'tip_articol' => 'asociat',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolAsociatId,
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleMainIdNew2,
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainIdNew2,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => $articolAsociatId,
        ]);
        $stiri_conectate = array_diff($stiri_conectate, [$articolAsociatId]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => implode(',', $stiri_conectate),
        ]);



        // #19 - Articol principal salvat ca articol asociat
        $articleMainToAsociatId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'România nu reușește să atragă banii din PNRR. Gradul de absorbție este de 0,3%, banii zac nefolosiţi în Trezorerie',
        ]);
        $articleMainToAsociat = $this->db->table('ip_articole_salvate')->where('id', $articleMainToAsociatId)->get()->getRow();
        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articleMainToAsociatId,
                'title_article' => $articleMainToAsociat->title,
                'abstract_article' => '' . $articleMainToAsociat->abstract,
                'source_article' => '' . $articleMainToAsociat->source,
                'link_article' => $articleMainToAsociat->articol_url,
                'caracter_articol' => $articleMainToAsociat->caracter_articol,
                'categorie_articol' => $articleMainToAsociat->categorie,
                'dedicat_articol' => $articleMainToAsociat->dedicat,
                'relevant_articol' => $articleMainToAsociat->relevant,
                'autor_articol' => $articleMainToAsociat->author,
                'publication_date' => $articleMainToAsociat->publication_date,
                'tip_articol' => 'asociat',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleMainToAsociatId,
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => '',
        ]);
        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articolAsociatId,
            'stire_originala' => '',
            'stiri_conectate' => '',
        ]);


    }

    public function testArticolPrincipalFaraArticoleAsociate()
    {
        $title = 'Poliţia de Frontieră: Peste 125.000 de persoane au intrat, duminică, în România, dintre care aproape 9.000 de cetăţeni ucraineni';
        $articleId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => $title,
        ]);
        $article = $this->db->table('ip_articole_salvate')->where('id', $articleId)->get()->getRow();
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articleId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => 'Pozitiv',
                'categorie_articol' => 'Romania',
                'dedicat_articol' => 'Mentionat',
                'relevant_articol' => 'Nerelevant',
                'autor_articol' => $article->author,
                'publication_date' => '2023-05-20 13:12:31',
                'stiri_conectate' => null,
                'tip_articol' => 'principal',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleId,
            'stire_originala' => 'articol principal',
            'stiri_conectate' => '',
        ]);
        $this->dontSeeInDatabase('ip_articole_salvate', [
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => $articleId,
        ]);
    }


    public function testArticolAsociatFaraArticolPrincipal()
    {
        $title = 'Poliţia de Frontieră: Peste 125.000 de persoane au intrat, duminică, în România, dintre care aproape 9.000 de cetăţeni ucraineni';
        $articleId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => $title,
        ]);
        $article = $this->db->table('ip_articole_salvate')->where('id', $articleId)->get()->getRow();
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withHeaders([
            'X-Requested-With' => 'XMLHttpRequest' // simulate ajax request
        ])->withSession($this->userSessionCompanie)
            ->call('post', '/articole/salveaza_nou'  , [
                'raport' => $raportId,
                'raport_single' => $raportSingleId,
                'articol_salvat' => $articleId,
                'title_article' => $article->title,
                'abstract_article' => '' . $article->abstract,
                'source_article' => '' . $article->source,
                'link_article' => $article->articol_url,
                'caracter_articol' => 'Pozitiv',
                'categorie_articol' => 'Romania',
                'dedicat_articol' => 'Mentionat',
                'relevant_articol' => 'Nerelevant',
                'autor_articol' => $article->author,
                'publication_date' => '2023-05-20 13:12:31',
                'stiri_conectate' => null,
                'tip_articol' => 'asociat',
            ]);

        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertEquals('success', $json['status']);

        $this->seeInDatabase('ip_articole_salvate', [
            'id' => $articleId,
            'stire_originala' => 'articol asociat',
            'stiri_conectate' => '',
        ]);

        $this->dontSeeInDatabase('ip_articole_salvate', [
            //'stire_originala' => 'articol principal',
            'stiri_conectate' => $articleId,
        ]);
    }

    public function testGenereazaRaport()
    {
        ///rapoarte/genereaza_raport/2/10
        $raportId = $this->grabFromDatabase('ip_rapoarte', 'id', [
            'id_user' => $this->userSessionCompanie['user_id'],
            'id_companie' => $this->userSessionCompanie['id_companie'],
            'name_raport' => $this->nameRaport,
        ]);
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);

        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/rapoarte/genereaza_raport/' . $raportId . '/' . $raportSingleId);

        $result->assertStatus(200);
        $result->assertSee('Raport monitorizare');
        $result->assertSee('Test 106 articles');

        // Check if associat with no principal is still shown in report
        $title = 'Poliţia de Frontieră: Peste 125.000 de persoane au intrat, duminică, în România, dintre care aproape 9.000 de cetăţeni ucraineni';
        $result->assertSee($title);

        $this->seeInDatabase('ip_rapoarte_single',[
            'id' => $raportSingleId,
            'status' => 'Raport generat',
        ]);
    }


    public function testStatisticiPage()
    {
        $raportSingleId = $this->grabFromDatabase('ip_rapoarte_single', 'id', [
            'name_raport' => 'Test 106 articles',
        ]);
        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/statistici/raport/' . $raportSingleId);

        $result->assertStatus(200);
        $result->assertSee('STATISTICI RAPORT Test 106 articles');
        $result->assertSee('Test 106 articles');

        $title = 'Poliţia de Frontieră: Peste 125.000 de persoane au intrat, duminică, în România, dintre care aproape 9.000 de cetăţeni ucraineni';
        $result->assertSee($title);


        $result = $this->withSession($this->userSessionCompanie)
            ->call('get', '/statistici/by_word?word=Romania&raport_single_id=' . $raportSingleId);
        $result->assertStatus(200);
        $json = json_decode($result->getJSON(), true);
        $this->assertTrue($json['success']);
        $this->assertEquals(19, $json['data'][0]['total_score']);
        $this->assertStringContainsString('România, prima calificare la Europeanul de Handbal, cu mari emoții, după aproape 30 de ani / Istoria „tricolorilor” la EURO și la Mondiale', $json['data'][0]['titlu_articol']);


        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/statistici/get_source_stats_words',[
                'source' => 'psnews.ro',
                'raport_single' => $raportSingleId,
            ]);
        $result->assertStatus(200);
        $result->assertSee(' Romania: <strong>x125');

        $articleId = $this->grabFromDatabase('ip_articole_salvate', 'id', [
            'title' => 'Editat - Ce salariu primeşte un muncitor din Sri Lanka, în România: „Sunt plătiti mai bine decât românii pentru că muncesc mai mult”',
            'id_raport_single' => $raportSingleId,
        ]);

        
        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/statistici/get_article_stats_words',[
                'article' => $articleId,
            ]);
        $result->assertStatus(200);

        $result->assertSee(' Romania: <strong>x7</strong>');


        $result = $this->withSession($this->userSessionCompanie)
            ->call('post', '/statistici/redoStatsRaport/' . $raportSingleId,[

            ]);
        $result->assertStatus(200);
        $result->assertSee('Romania: <strong>x319</strong>');
    }




}