<commit_before><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic, NP. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     MIT http://opensource.org/licenses/MIT
 */

namespace Mautic\Tests\Api;

use Mautic\Api\Contacts;

class ContactsTest extends AbstractCustomFieldsTest
{
    protected $skipPayloadAssertion = array('firstname', 'lastname', 'tags');

    /**
     * @var Contacts
     */
    protected $api;

    public function setUp()
    {
        $this->api = $this->getContext('contacts');
        $this->testPayload = array(
            'firstname' => 'John',
            'lastname'  => 'APIDoe',
            'address2'  => 'Sam & Sons',
            'email'     => '<EMAIL>',
            'points'    => 3,
            'tags'      => array(
                'APItag1',
                'APItag2',
            )
        );
    }

    protected function assertEventResponse($response, $expectedEvents = null)
    {
        $this->assertErrors($response);
        $this->assertTrue(isset($response['events']));
        $this->assertTrue(isset($response['total']));
        $this->assertTrue(isset($response['types']));
        $this->assertTrue(isset($response['order']));
        $this->assertTrue(isset($response['filters']));

        if ($expectedEvents) {
            foreach ($expectedEvents as $key => $eventName) {
                $actual = 'oops Missing';
                foreach ($response['events'] as $event) {
                    if ($eventName == $event['event']) {
                        $actual = $event['event'];
                        break;
                    }
                }
                $this->assertEquals($eventName, $actual);
            }
        }
    }

    public function testGetList()
    {
        $this->standardTestGetList();
    }

    /**
     * We cannot use standard method since contacts with the same email are merged into one
     */
    public function testGetListOfSpecificIds()
    {
        // Create some items first
        $itemIds = array();
        for ($i = 0; $i <= 2; $i++) {
            $testPayload = $this->testPayload;
            $testPayload['email'] = $i.$this->testPayload['email'];
            $response = $this->api->create($testPayload);
            $this->assertErrors($response);
            $itemIds[] = $response[$this->api->itemName()]['id'];
        }

        $search   = 'ids:'.implode(',', $itemIds);
        $response = $this->api->getList($search);
        $this->assertErrors($response);
        $this->assertEquals(count($itemIds), $response['total']);

        foreach ($response[$this->api->listName()] as $item) {
            $this->assertTrue(in_array($item['id'], $itemIds));
            $this->api->delete($item['id']);
            $this->assertErrors($response);
        }
    }

    public function testGetListOfSpecificSegment()
    {

        $segmentApi = $this->getContext('segments');

        // Create Segment
        $segmentPayload = array(
            'name' => 'Contact Segment Search API test'
        );
        $response = $segmentApi->create($segmentPayload);
        $this->assertErrors($response);
        $segmentId = $response['list']['id'];
        $segmentAlias = $response['list']['alias'];

        $itemIds = array();
        for ($i = 0; $i <= 2; $i++) {

            $testPayload = $this->testPayload;
            $testPayload['email'] = $i.$this->testPayload['email'];

            // Create some items
            $response = $this->api->create($testPayload);
            $this->assertErrors($response);
            $itemIds[] = $response[$this->api->itemName()]['id'];

            // Add contacts to the segment
            $response = $segmentApi->addContact($segmentId, $response[$this->api->itemName()]['id']);
            $this->assertErrors($response);
        }

        $search = 'segment:'.$segmentAlias;
        $response = $this->api->getList($search);
        $this->assertErrors($response);
        $this->assertEquals(count($itemIds), $response['total']);

        foreach ($response[$this->api->listName()] as $item) {
            $this->assertTrue(in_array($item['id'], $itemIds));
            $this->api->delete($item['id']);
            $this->assertErrors($response);
        }

        $segmentApi->delete($segmentId);
        $this->assertErrors($response);
    }

    public function testGetFieldList()
    {
        $response    = $this->api->getFieldList();
        $this->assertErrors($response);
        $this->assertGreaterThan(0, count($response));
    }

    public function testGetSegmentsList()
    {
        $response    = $this->api->getSegments();
        $this->assertErrors($response);
    }

    public function testGetEvents()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getEvents($contact['id']);
        $this->assertEventResponse($response, array('lead.create', 'lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetEventsAdvanced()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getEvents($contact['id'], '', array('lead.identified'));
        $this->assertEventResponse($response, array('lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivityForContact()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getActivityForContact($contact['id']);
        $this->assertEventResponse($response, array('lead.create', 'lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivityForContactAdvanced()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getActivityForContact($contact['id'], '', array('lead.identified'));
        $this->assertEventResponse($response, array('lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivity()
    {
        $response = $this->api->getActivity();
        $this->assertEventResponse($response, null);
    }

    public function testGetActivityAdvanced()
    {
        $response = $this->api->getActivity('', array('page.hit'));
        $this->assertEventResponse($response, array('page.hit'));
    }<commit_msg>lead.create and lead.identified are excluded from API calls at Mautic's end. So to test events lets create some events first<commit_after><?php
/**
 * @package     Mautic
 * @copyright   2014 Mautic, NP. All rights reserved.
 * @author      Mautic
 * @link        http://mautic.org
 * @license     MIT http://opensource.org/licenses/MIT
 */

namespace Mautic\Tests\Api;

use Mautic\Api\Contacts;

class ContactsTest extends AbstractCustomFieldsTest
{
    protected $skipPayloadAssertion = array('firstname', 'lastname', 'tags');

    /**
     * @var Contacts
     */
    protected $api;

    public function setUp()
    {
        $this->api = $this->getContext('contacts');
        $this->testPayload = array(
            'firstname' => 'John',
            'lastname'  => 'APIDoe',
            'address2'  => 'Sam & Sons',
            'email'     => '<EMAIL>',
            'points'    => 3,
            'tags'      => array(
                'APItag1',
                'APItag2',
            )
        );
    }

    protected function assertEventResponse($response, $expectedEvents = null)
    {
        $this->assertErrors($response);
        $this->assertTrue(isset($response['events']));
        $this->assertTrue(isset($response['total']));
        $this->assertTrue(isset($response['types']));
        $this->assertTrue(isset($response['order']));
        $this->assertTrue(isset($response['filters']));

        if ($expectedEvents) {
            foreach ($expectedEvents as $key => $eventName) {
                $actual = 'oops Missing';
                foreach ($response['events'] as $event) {
                    if ($eventName == $event['event']) {
                        $actual = $event['event'];
                        break;
                    }
                }
                $this->assertEquals($eventName, $actual);
            }
        }
    }

    public function testGetList()
    {
        $this->standardTestGetList();
    }

    /**
     * We cannot use standard method since contacts with the same email are merged into one
     */
    public function testGetListOfSpecificIds()
    {
        // Create some items first
        $itemIds = array();
        for ($i = 0; $i <= 2; $i++) {
            $testPayload = $this->testPayload;
            $testPayload['email'] = $i.$this->testPayload['email'];
            $response = $this->api->create($testPayload);
            $this->assertErrors($response);
            $itemIds[] = $response[$this->api->itemName()]['id'];
        }

        $search   = 'ids:'.implode(',', $itemIds);
        $response = $this->api->getList($search);
        $this->assertErrors($response);
        $this->assertEquals(count($itemIds), $response['total']);

        foreach ($response[$this->api->listName()] as $item) {
            $this->assertTrue(in_array($item['id'], $itemIds));
            $this->api->delete($item['id']);
            $this->assertErrors($response);
        }
    }

    public function testGetListOfSpecificSegment()
    {

        $segmentApi = $this->getContext('segments');

        // Create Segment
        $segmentPayload = array(
            'name' => 'Contact Segment Search API test'
        );
        $response = $segmentApi->create($segmentPayload);
        $this->assertErrors($response);
        $segmentId = $response['list']['id'];
        $segmentAlias = $response['list']['alias'];

        $itemIds = array();
        for ($i = 0; $i <= 2; $i++) {

            $testPayload = $this->testPayload;
            $testPayload['email'] = $i.$this->testPayload['email'];

            // Create some items
            $response = $this->api->create($testPayload);
            $this->assertErrors($response);
            $itemIds[] = $response[$this->api->itemName()]['id'];

            // Add contacts to the segment
            $response = $segmentApi->addContact($segmentId, $response[$this->api->itemName()]['id']);
            $this->assertErrors($response);
        }

        $search = 'segment:'.$segmentAlias;
        $response = $this->api->getList($search);
        $this->assertErrors($response);
        $this->assertEquals(count($itemIds), $response['total']);

        foreach ($response[$this->api->listName()] as $item) {
            $this->assertTrue(in_array($item['id'], $itemIds));
            $this->api->delete($item['id']);
            $this->assertErrors($response);
        }

        $segmentApi->delete($segmentId);
        $this->assertErrors($response);
    }

    public function testGetFieldList()
    {
        $response    = $this->api->getFieldList();
        $this->assertErrors($response);
        $this->assertGreaterThan(0, count($response));
    }

    public function testGetSegmentsList()
    {
        $response    = $this->api->getSegments();
        $this->assertErrors($response);
    }

    public function testGetEvents()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getEvents($contact['id']);
        $this->assertEventResponse($response, array('lead.create', 'lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetEventsAdvanced()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        $response = $this->api->getEvents($contact['id'], '', array('lead.identified'));
        $this->assertEventResponse($response, array('lead.identified'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivityForContact()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        // Add some activity
        $this->api->addDnc($contact['id'], 'email', Contacts::BOUNCED);
        $this->api->addPoints($contact['id'], 3);

        $response = $this->api->getActivityForContact($contact['id']);
        $this->assertEventResponse($response, array('lead.donotcontact', 'point.gained'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivityForContactAdvanced()
    {
        $response = $this->api->create($this->testPayload);
        $this->assertErrors($response);
        $contact = $response[$this->api->itemName()];

        // Add some activity
        $this->api->addDnc($contact['id'], 'email', Contacts::BOUNCED);
        $this->api->addPoints($contact['id'], 3);

        $response = $this->api->getActivityForContact($contact['id'], '', array('lead.donotcontact'));
        $this->assertEventResponse($response, array('lead.donotcontact'));

        $response = $this->api->delete($contact['id']);
        $this->assertErrors($response);
    }

    public function testGetActivity()
    {
        $response = $this->api->getActivity();
        $this->assertEventResponse($response, null);
    }

    public function testGetActivityAdvanced()
    {
        $response = $this->api->getActivity('', array('page.hit'));
        $this->assertEventResponse($response, array('page.hit'));
    }
{'fraction_non_alphanumeric': 0.17795781533733762, 'fraction_numerical': 0.0019555803883223913, 'mean_word_length': 2.4280584151304763, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 259, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '211557', 'n_tokens_mistral': 4024, 'n_tokens_neox': 3897, 'n_words': 655}