You are here

menu_link.test in Menu Link (Field) 7

Tests for Menu link module.

File

menu_link.test
View source
<?php

/**
 * @file
 * Tests for Menu link module.
 */

/**
 * Tests for menu link field and formatter.
 */
class MenuLinkFieldTestCase extends DrupalWebTestCase {
  protected $instance;
  public static function getInfo() {
    return array(
      'name' => 'Menu link field',
      'description' => 'Test the creation of menu link fields.',
      'group' => 'Menu link',
    );
  }
  function setUp() {
    parent::setUp('field_test');
    $web_user = $this
      ->drupalCreateUser(array(
      'access field_test content',
      'administer field_test content',
    ));
    $this
      ->drupalLogin($web_user);

    // Setup a field and instance.
    $this->field_name = drupal_strtolower($this
      ->randomName());
    $this->field = array(
      'field_name' => $this->field_name,
      'type' => 'menu_link',
    );
    field_create_field($this->field);
    $this->instance = array(
      'field_name' => $this->field_name,
      'entity_type' => 'test_entity',
      'bundle' => 'test_bundle',
      'setings' => array(
        'menu_options' => array(
          'main-menu',
        ),
      ),
      'widget' => array(
        'type' => 'menu_link_default',
      ),
      'display' => array(
        'full' => array(
          'type' => 'menu_link_link',
        ),
      ),
    );
    field_create_instance($this->instance);
  }

  /**
   * Test menu link field validation.
   */
  function testMenuLinkFieldValidation() {

    // Test valid and invalid values with field_attach_validate().
    $langcode = LANGUAGE_NONE;
    $entity = field_test_create_stub_entity();
    $entity->uri = array(
      'path' => 'test',
      'options' => array(),
    );
    $entity->{$this->field_name}[$langcode][0] = array(
      'menu_name' => 'main-menu',
      'plid' => 0,
      'link_title' => 'Title',
    );
    try {
      field_attach_validate('test_entity', $entity);
      $this
        ->pass(t('Correct menu link does not cause validation error'));
    } catch (FieldValidationException $e) {
      $this
        ->fail(t('Correct menu link does not cause validation error'));
    }
    $entity = field_test_create_stub_entity();
    $entity->uri = array(
      'path' => 'test',
      'options' => array(),
    );
    $entity->{$this->field_name}[$langcode][0] = array(
      'menu_name' => 'nonexistant-menu',
      'plid' => 0,
      'link_title' => 'Title',
    );
    try {
      field_attach_validate('test_entity', $entity);
      $this
        ->fail(t('Menu link with an invalid menu name causes validation error'));
    } catch (FieldValidationException $e) {
      $this
        ->pass(t('Menu link with an invalid menu name causes validation error'));
    }
  }

}

Classes

Namesort descending Description
MenuLinkFieldTestCase Tests for menu link field and formatter.