You are here

TelephoneFormatterFunctionalTest.test in Telephone Formatter 7

File

tests/TelephoneFormatterFunctionalTest.test
View source
<?php

use libphonenumber\PhoneNumberFormat;
class TelephoneFormatterFunctionalTest extends DrupalWebTestCase {
  protected $privilegedUser;
  public static function getInfo() {
    return array(
      'name' => 'Telephone Formatter Functional Tests',
      'description' => 'Tests the creation of telephone fields.',
      'group' => 'Telephone Formatter',
    );
  }
  public function setUp() {
    parent::setUp('telephone_formatter');
    $this->privilegedUser = $this
      ->drupalCreateUser(array(
      'create page content',
      'edit own page content',
    ));
    $this
      ->drupalLogin($this->privilegedUser);
  }

  /**
   * Helper function for testTelephoneField().
   */
  public function testTelephoneFieldFallback() {
    $this
      ->generateTelephoneField();
    $node = $this
      ->drupalCreateNode([
      'field_telephone' => [
        '98765432',
      ],
    ]);
    $this
      ->drupalGet('node/' . $node->nid);
    $this
      ->assertText(98765432, 'Telephone number was found on the created node.');
  }

  /**
   * Helper method for telephone field generation.
   */
  protected function generateTelephoneField($settings = array()) {

    // Add the telephone field to the article content type.
    $field_name = 'field_telephone';
    $field = array(
      'field_name' => $field_name,
      'type' => 'telephone',
    );
    field_create_field($field);
    $instance = array(
      'field_name' => $field_name,
      'entity_type' => 'node',
      'bundle' => 'page',
      'label' => 'Telephone Number',
    );
    field_create_instance($instance);
    $instance_info = field_info_instance('node', 'field_telephone', 'page');
    $instance_info['display']['default'] = $settings + array(
      'format' => PhoneNumberFormat::INTERNATIONAL,
      'link' => TRUE,
      'default_country' => NULL,
    );
    field_update_instance($instance_info);
  }

}