<?php

require_once __DIR__ . '../../BaseTestClass.php';

use Civi\Test\HeadlessInterface;
use Civi\Test\HookInterface;
use Civi\Test\TransactionalInterface;

/**
 * Test contribution DetailExtended class.
 *
 * Tips:
 *  - With HookInterface, you may implement CiviCRM hooks directly in the test class.
 *    Simply create corresponding functions (e.g. "hook_civicrm_post(...)" or similar).
 *  - With TransactionalInterface, any data changes made by setUp() or test****() functions will
 *    rollback automatically -- as long as you don't manipulate schema or truncate tables.
 *    If this test needs to manipulate schema or truncate tables, then either:
 *       a. Do all that using setupHeadless() and Civi\Test.
 *       b. Disable TransactionalInterface, and handle all setup/teardown yourself.
 *
 * @group headless
 */
class Contact_BasicContactTest extends BaseTestClass implements HeadlessInterface, HookInterface, TransactionalInterface {

  protected $contacts = [];

  /**
   * @throws \CRM_Core_Exception
   */
  public function setUp() {
    parent::setUp();
    $this->createCustomGroupWithField(['CustomField' => ['html_type' => 'CheckBox', 'option_values' => ['two' => 'A couple', 'three' => 'A few', 'four' => 'Too Many']]]);
    $contact = $this->callAPISuccess('Contact', 'create', ['organization_name' => 'Amazons', 'last_name' => 'Woman', 'contact_type' => 'Organization', 'custom_' . $this->customFieldID => 'three']);

    $this->contacts[] = $contact['id'];
    $contact = $this->callAPISuccess('Contact', 'create', [
      'first_name' => 'Wonder',
      'last_name' => 'Woman',
      'contact_type' => 'Individual',
      'employer_id' => $contact['id'],
      'custom_' . $this->customFieldID => 'two',
      'gender_id' => 'Female',
    ]);
    $this->contacts[] = $contact['id'];
  }

  /**
   * @throws \CRM_Core_Exception
   */
  public function tearDown() {
    parent::tearDown();
    $fields = $this->callAPISuccess('CustomField', 'get', ['custom_group_id' => $this->customGroupID])['values'];
    foreach ($fields as $field) {
      $this->callAPISuccess('CustomField', 'delete', ['id' => $field['id']]);
    }

    $this->callAPISuccess('CustomGroup', 'delete', ['id' => $this->customGroupID]);
    foreach ($this->contacts as $contact) {
      $this->callAPISuccess('Contact', 'delete', ['id' => $contact]);
    }
    CRM_Core_DAO::executeQuery('DELETE FROM civicrm_cache');
    CRM_Core_PseudoConstant::flush();
  }

  /**
   * Test custom field filter works.
   *
   * @throws \CRM_Core_Exception
   */
  public function testCustomFieldFilter() {
    $customField = $this->customFieldCreate(['html_type' => 'Autocomplete-Select', 'data_type' => 'ContactReference', 'default_value' => '', 'custom_group_id' => $this->customGroupID]);
    $this->callAPISuccess('Contact', 'create', ['custom_' . $customField['id'] => $this->contacts[0], 'id' => $this->contacts['1']]);
    $params = [
      'report_id' => 'contact/contactbasic',
      'custom_' . $customField['id'] . '_value' => 'Amaz',
      'custom_' . $customField['id'] . '_op' => 'has',
      'fields' => [
        'civicrm_contact_display_name' => '1',
        'civicrm_contact_contact_id' => '1',
        'custom_' . $customField['id'] => '1',
      ],
    ];
    $rows = $this->getRows($params);
    $this->assertEquals([
      0 =>
        [
          'civicrm_contact_civicrm_contact_display_name' => 'Wonder Woman',
          'civicrm_contact_civicrm_contact_contact_id' => $this->contacts[1],
          'contact_custom_' . $customField['id'] . '_civireport' => 'Amazons',
          'civicrm_contact_civicrm_contact_contact_id_link' => '/index.php?q=civicrm/contact/view&amp;reset=1&amp;cid=' . $this->contacts[1],
        ],
    ], $rows);
    $params['custom_' . $customField['id'] . '_value'] = 'Wonder';
    $rows = $this->getRows($params);
    $this->assertEmpty($rows);
  }

}
