<commit_before><?php

class LeadTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        global $current_user;
        get_sugar_config_defaults();
        $current_user = new User();
    }

    public function testLead()
    {

        //execute the contructor and check for the Object type and  attributes
        $lead = new Lead();

        $this->assertInstanceOf('Lead', $lead);
        $this->assertInstanceOf('Person', $lead);
        $this->assertInstanceOf('SugarBean', $lead);

        $this->assertAttributeEquals('Leads', 'module_dir', $lead);
        $this->assertAttributeEquals('Lead', 'object_name', $lead);
        $this->assertAttributeEquals('Leads', 'object_names', $lead);
        $this->assertAttributeEquals('leads', 'table_name', $lead);

        $this->assertAttributeEquals(true, 'new_schema', $lead);
        $this->assertAttributeEquals(true, 'importable', $lead);
    }

    public function testget_account()
    {
        error_reporting(E_ERROR | E_PARSE);

        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_account();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->account_id = 1;
        $result = $lead->get_account();
        $this->assertEquals(null, $result);
    }

    public function testget_opportunity()
    {
        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_opportunity();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->opportunity_id = 1;
        $result = $lead->get_opportunity();
        $this->assertEquals(null, $result);
    }

    public function testget_contact()
    {
        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_contact();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->contact_id = 1;
        $result = $lead->get_contact();
        $this->assertEquals(null, $result);
    }

    public function testcreate_list_query()
    {
        $this->markTestIncomplete('Breaks on php 7.1');
        $lead = new Lead();

        //test with empty string params
        $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads 			LEFT JOIN users\n                                ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id)  LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where  leads.deleted=0 ";
        $actual = $lead->create_list_query('', '');
        $this->assertSame($expected, $actual);


        //test with valid string params
        $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads 			LEFT JOIN users\n                                ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id)  LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where (users.user_name=\"\") AND  leads.deleted=0  ORDER BY leads.id";
        $actual = $lead->create_list_query('leads.id', 'users.user_name=""');
        $this->assertSame($expected, $actual);
    }

    /**
     * @todo: NEEDS FIXING!
     */
    public function testcreate_new_list_query()
    {
        /*
        $lead = new Lead();

        //test with empty string params
        $expected = " SELECT  leads.* , '                                                                                                                                                                                                                                                              ' c_accept_status_fields , '                                    '  call_id , '                                                                                                                                                                                                                                                              ' e_invite_status_fields , '                                    '  fp_events_leads_1fp_events_ida , '                                                                                                                                                                                                                                                              ' e_accept_status_fields , LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as name , jt3.user_name modified_by_name , jt3.created_by modified_by_name_owner  , 'Users' modified_by_name_mod , jt4.user_name created_by_name , jt4.created_by created_by_name_owner  , 'Users' created_by_name_mod , jt5.user_name assigned_user_name , jt5.created_by assigned_user_name_owner  , 'Users' assigned_user_name_mod, LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as full_name , jt6.name campaign_name , jt6.assigned_user_id campaign_name_owner  , 'Campaigns' campaign_name_mod, '                                                                                                                                                                                                                                                              ' m_accept_status_fields , '                                    '  meeting_id  FROM leads   LEFT JOIN  users jt3 ON leads.modified_user_id=jt3.id AND jt3.deleted=0\n\n AND jt3.deleted=0  LEFT JOIN  users jt4 ON leads.created_by=jt4.id AND jt4.deleted=0\n\n AND jt4.deleted=0  LEFT JOIN  users jt5 ON leads.assigned_user_id=jt5.id AND jt5.deleted=0\n\n AND jt5.deleted=0  LEFT JOIN  campaigns jt6 ON leads.campaign_id=jt6.id AND jt6.deleted=0\n\n AND jt6.deleted=0 where leads.deleted=0";
        $actual = $lead->create_new_list_query('','');
        $this->assertSame($expected,$actual);


        //test with valid string params
        $expected = " SELECT  leads.* , '                                                                                                                                                                                                                                                              ' c_accept_status_fields , '                                    '  call_id , '                                                                                                                                                                                                                                                              ' e_invite_status_fields , '                                    '  fp_events_leads_1fp_events_ida , '                                                                                                                                                                                                                                                              ' e_accept_status_fields , LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as name , jt3.user_name modified_by_name , jt3.created_by modified_by_name_owner  , 'Users' modified_by_name_mod , jt4.user_name created_by_name , jt4.created_by created_by_name_owner  , 'Users' created_by_name_mod , jt5.user_name assigned_user_name , jt5.created_by assigned_user_name_owner  , 'Users' assigned_user_name_mod, LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as full_name , jt6.name campaign_name , jt6.assigned_user_id campaign_name_owner  , 'Campaigns' campaign_name_mod, '                                                                                                                                                                                                                                                              ' m_accept_status_fields , '                                    '  meeting_id  FROM leads   LEFT JOIN  users jt3 ON leads.modified_user_id=jt3.id AND jt3.deleted=0\n\n AND jt3.deleted=0  LEFT JOIN  users jt4 ON leads.created_by=jt4.id AND jt4.deleted=0\n\n AND jt4.deleted=0  LEFT JOIN  users jt5 ON leads.assigned_user_id=jt5.id AND jt5.deleted=0\n\n AND jt5.deleted=0  LEFT JOIN  campaigns jt6 ON leads.campaign_id=jt6.id AND jt6.deleted=0\n\n AND jt6.deleted=0 where (users.user_name=\"\") AND leads.deleted=0";
        $actual = $lead->create_new_list_query('leads.id','users.user_name=""');
        $this->assertSame($expected,$actual);
        */
        $this->assertTrue(true, "NEEDS FIXING!");
    }

    public function testSaveAndConverted_lead()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastnn";
        $lead->lead_source = "test";

        $result = $lead->save();

        //test for record ID to verify that record is saved
        $this->assertTrue(isset($lead->id));
        $this->assertEquals(36, strlen($lead->id));
        $this->assertEquals("New", $lead->status);


        //test converted_lead method after saving
        /*$lead->converted_lead("'" . $lead->id . "'" , "'1'", "'1'", "'1'");

        //retrieve back to test if attributes are updated in db
        $lead = $lead->retrieve($lead->id);
        $this->assertEquals("Converted", $lead->status);
        $this->assertEquals("1", $lead->converted);
        $this->assertEquals("1", $lead->contact_id);
        $this->assertEquals("1", $lead->account_id);
        $this->assertEquals("1", $lead->opportunity_id);
        */

        $this->markTestSkipped("converted_lead: Error in query, id's not properly escaped ");


        //mark the record as deleted and verify that this record cannot be retrieved anymore.
        $lead->mark_deleted($lead->id);
        $result = $lead->retrieve($lead->id);
        $this->assertEquals(null, $result);
    }


    public function testfill_in_additional_list_fields()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastn";

        $lead->fill_in_additional_list_fields();

        $this->assertEquals("firstn lastn", $lead->name);
    }


    public function testfill_in_additional_detail_fields()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastn";

        $lead->fill_in_additional_detail_fields();

        $this->assertEquals("firstn lastn", $lead->name);
    }

    public function testget_list_view_data()
    {
        $lead = new Lead();

        $expected = array(
            'NAME' => ' ',
            'DELETED' => 0,
            'FULL_NAME' => ' ',
            'DO_NOT_CALL' => '0',
            'CONVERTED' => '0',
            'ENCODED_NAME' => ' ',
            'EMAIL1' => '',
            'EMAIL1_LINK' => '<a class="email-link" href="javascript:void(0);"  onclick=" $(document).openComposeViewModal(this);" data-module="Leads" data-record-id="" data-module-name=" "  data-email-address="">',
            'ACC_NAME_FROM_ACCOUNTS' => null,
        );

        $actual = $lead->get_list_view_data();

        //$this->assertSame($expected, $actual);
        $this->assertEquals($expected['NAME'], $actual['NAME']);
        $this->assertEquals($expected['DELETED'], $actual['DELETED']);
        $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']);
        $this->assertEquals($expected['DO_NOT_CALL'], $actual['DO_NOT_CALL']);
        $this->assertEquals($expected['EMAIL1_LINK'], $actual['EMAIL1_LINK']);
    }


    public function testget_linked_fields()
    {
        $lead = new Lead();

        $expected = array(
            'created_by_link',
            'modified_user_link',<commit_msg>Updating Lead Test<commit_after><?php

class LeadTest extends PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        global $current_user;
        get_sugar_config_defaults();
        $current_user = new User();
    }

    public function testLead()
    {

        //execute the contructor and check for the Object type and  attributes
        $lead = new Lead();

        $this->assertInstanceOf('Lead', $lead);
        $this->assertInstanceOf('Person', $lead);
        $this->assertInstanceOf('SugarBean', $lead);

        $this->assertAttributeEquals('Leads', 'module_dir', $lead);
        $this->assertAttributeEquals('Lead', 'object_name', $lead);
        $this->assertAttributeEquals('Leads', 'object_names', $lead);
        $this->assertAttributeEquals('leads', 'table_name', $lead);

        $this->assertAttributeEquals(true, 'new_schema', $lead);
        $this->assertAttributeEquals(true, 'importable', $lead);
    }

    public function testget_account()
    {
        error_reporting(E_ERROR | E_PARSE);

        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_account();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->account_id = 1;
        $result = $lead->get_account();
        $this->assertEquals(null, $result);
    }

    public function testget_opportunity()
    {
        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_opportunity();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->opportunity_id = 1;
        $result = $lead->get_opportunity();
        $this->assertEquals(null, $result);
    }

    public function testget_contact()
    {
        $lead = new Lead();

        //test without pre settting attributes
        $result = $lead->get_contact();
        $this->assertEquals(null, $result);


        //test with required attributes preset
        $lead->contact_id = 1;
        $result = $lead->get_contact();
        $this->assertEquals(null, $result);
    }

    public function testcreate_list_query()
    {
        $this->markTestIncomplete('Breaks on php 7.1');
        $lead = new Lead();

        //test with empty string params
        $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads 			LEFT JOIN users\n                                ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id)  LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where  leads.deleted=0 ";
        $actual = $lead->create_list_query('', '');
        $this->assertSame($expected, $actual);


        //test with valid string params
        $expected = "SELECT leads.*, users.user_name assigned_user_name,leads_cstm.* FROM leads 			LEFT JOIN users\n                                ON leads.assigned_user_id=users.id LEFT JOIN email_addr_bean_rel eabl  ON eabl.bean_id = leads.id AND eabl.bean_module = 'Leads' and eabl.primary_address = 1 and eabl.deleted=0 LEFT JOIN email_addresses ea ON (ea.id = eabl.email_address_id)  LEFT JOIN leads_cstm ON leads.id = leads_cstm.id_c where (users.user_name=\"\") AND  leads.deleted=0  ORDER BY leads.id";
        $actual = $lead->create_list_query('leads.id', 'users.user_name=""');
        $this->assertSame($expected, $actual);
    }

    /**
     * @todo: NEEDS FIXING!
     */
    public function testcreate_new_list_query()
    {
        /*
        $lead = new Lead();

        //test with empty string params
        $expected = " SELECT  leads.* , '                                                                                                                                                                                                                                                              ' c_accept_status_fields , '                                    '  call_id , '                                                                                                                                                                                                                                                              ' e_invite_status_fields , '                                    '  fp_events_leads_1fp_events_ida , '                                                                                                                                                                                                                                                              ' e_accept_status_fields , LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as name , jt3.user_name modified_by_name , jt3.created_by modified_by_name_owner  , 'Users' modified_by_name_mod , jt4.user_name created_by_name , jt4.created_by created_by_name_owner  , 'Users' created_by_name_mod , jt5.user_name assigned_user_name , jt5.created_by assigned_user_name_owner  , 'Users' assigned_user_name_mod, LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as full_name , jt6.name campaign_name , jt6.assigned_user_id campaign_name_owner  , 'Campaigns' campaign_name_mod, '                                                                                                                                                                                                                                                              ' m_accept_status_fields , '                                    '  meeting_id  FROM leads   LEFT JOIN  users jt3 ON leads.modified_user_id=jt3.id AND jt3.deleted=0\n\n AND jt3.deleted=0  LEFT JOIN  users jt4 ON leads.created_by=jt4.id AND jt4.deleted=0\n\n AND jt4.deleted=0  LEFT JOIN  users jt5 ON leads.assigned_user_id=jt5.id AND jt5.deleted=0\n\n AND jt5.deleted=0  LEFT JOIN  campaigns jt6 ON leads.campaign_id=jt6.id AND jt6.deleted=0\n\n AND jt6.deleted=0 where leads.deleted=0";
        $actual = $lead->create_new_list_query('','');
        $this->assertSame($expected,$actual);


        //test with valid string params
        $expected = " SELECT  leads.* , '                                                                                                                                                                                                                                                              ' c_accept_status_fields , '                                    '  call_id , '                                                                                                                                                                                                                                                              ' e_invite_status_fields , '                                    '  fp_events_leads_1fp_events_ida , '                                                                                                                                                                                                                                                              ' e_accept_status_fields , LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as name , jt3.user_name modified_by_name , jt3.created_by modified_by_name_owner  , 'Users' modified_by_name_mod , jt4.user_name created_by_name , jt4.created_by created_by_name_owner  , 'Users' created_by_name_mod , jt5.user_name assigned_user_name , jt5.created_by assigned_user_name_owner  , 'Users' assigned_user_name_mod, LTRIM(RTRIM(CONCAT(IFNULL(leads.first_name,''),' ',IFNULL(leads.last_name,'')))) as full_name , jt6.name campaign_name , jt6.assigned_user_id campaign_name_owner  , 'Campaigns' campaign_name_mod, '                                                                                                                                                                                                                                                              ' m_accept_status_fields , '                                    '  meeting_id  FROM leads   LEFT JOIN  users jt3 ON leads.modified_user_id=jt3.id AND jt3.deleted=0\n\n AND jt3.deleted=0  LEFT JOIN  users jt4 ON leads.created_by=jt4.id AND jt4.deleted=0\n\n AND jt4.deleted=0  LEFT JOIN  users jt5 ON leads.assigned_user_id=jt5.id AND jt5.deleted=0\n\n AND jt5.deleted=0  LEFT JOIN  campaigns jt6 ON leads.campaign_id=jt6.id AND jt6.deleted=0\n\n AND jt6.deleted=0 where (users.user_name=\"\") AND leads.deleted=0";
        $actual = $lead->create_new_list_query('leads.id','users.user_name=""');
        $this->assertSame($expected,$actual);
        */
        $this->assertTrue(true, "NEEDS FIXING!");
    }

    public function testSaveAndConverted_lead()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastnn";
        $lead->lead_source = "test";

        $result = $lead->save();

        //test for record ID to verify that record is saved
        $this->assertTrue(isset($lead->id));
        $this->assertEquals(36, strlen($lead->id));
        $this->assertEquals("New", $lead->status);


        //test converted_lead method after saving
        /*$lead->converted_lead("'" . $lead->id . "'" , "'1'", "'1'", "'1'");

        //retrieve back to test if attributes are updated in db
        $lead = $lead->retrieve($lead->id);
        $this->assertEquals("Converted", $lead->status);
        $this->assertEquals("1", $lead->converted);
        $this->assertEquals("1", $lead->contact_id);
        $this->assertEquals("1", $lead->account_id);
        $this->assertEquals("1", $lead->opportunity_id);
        */

        $this->markTestSkipped("converted_lead: Error in query, id's not properly escaped ");


        //mark the record as deleted and verify that this record cannot be retrieved anymore.
        $lead->mark_deleted($lead->id);
        $result = $lead->retrieve($lead->id);
        $this->assertEquals(null, $result);
    }


    public function testfill_in_additional_list_fields()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastn";

        $lead->fill_in_additional_list_fields();

        $this->assertEquals("firstn lastn", $lead->name);
    }


    public function testfill_in_additional_detail_fields()
    {
        $lead = new Lead();

        $lead->first_name = "firstn";
        $lead->last_name = "lastn";

        $lead->fill_in_additional_detail_fields();

        $this->assertEquals("firstn lastn", $lead->name);
    }

    public function testget_list_view_data()
    {
        $lead = new Lead();

        $expected = array(
            'NAME' => ' ',
            'DELETED' => 0,
            'FULL_NAME' => ' ',
            'DO_NOT_CALL' => '0',
            'CONVERTED' => '0',
            'ENCODED_NAME' => ' ',
            'EMAIL1' => '',
            'EMAIL1_LINK' => '<a class="email-link" href="javascript:void(0);"  onclick=" $(document).openComposeViewModal(this);" data-module="Leads" data-record-id="" data-module-name=" "  data-email-address=""><span class="email-opt-in-container">
            <span class="email-opt-in email-opt-in-not-sent" title="Pending Confirm opt in, Confirm opt in not sent">✓</span>
    </span>',
            'ACC_NAME_FROM_ACCOUNTS' => null,
        );

        $actual = $lead->get_list_view_data();

        //$this->assertSame($expected, $actual);
        $this->assertEquals($expected['NAME'], $actual['NAME']);
        $this->assertEquals($expected['DELETED'], $actual['DELETED']);
        $this->assertEquals($expected['FULL_NAME'], $actual['FULL_NAME']);
        $this->assertEquals($expected['DO_NOT_CALL'], $actual['DO_NOT_CALL']);
        $this->assertEquals($expected['EMAIL1_LINK'], $actual['EMAIL1_LINK']);
    }


    public function testget_linked_fields()
    {
        $lead = new Lead();

        $expected = array(
            'created_by_link',
            'modified_user_link',
{'fraction_non_alphanumeric': 0.11475, 'fraction_numerical': 0.008, 'mean_word_length': 1.5991986138184968, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 189, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 220, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7471385', 'n_tokens_mistral': 6691, 'n_tokens_neox': 6356, 'n_words': 1267}