You are here

function NodeDisplaysFields::testNdCustomFields in Node displays 6.2

Same name and namespace in other branches
  1. 6.3 tests/nd.fields.test \NodeDisplaysFields::testNdCustomFields()
  2. 6 tests/nd.fields.test \NodeDisplaysFields::testNdCustomFields()
  3. 7 tests/nd.fields.test \NodeDisplaysFields::testNdCustomFields()

Test custom fields.

File

tests/nd.fields.test, line 55
Tests for Node displays (fields)

Class

NodeDisplaysFields
@file Tests for Node displays (fields)

Code

function testNdCustomFields() {
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer nodes',
    'access display suite',
    'administer nd',
    'configure layout for nd',
    'export and import settings',
    'revert overridden settings',
    'use PHP in custom fields',
  ));
  $this
    ->drupalLogin($admin_user);

  // Fields
  $this
    ->drupalGet(DS_PATH_MODULES . '/nd/fields');

  // Test being empty.
  $fields = variable_get('nd_fields', array());
  $this
    ->assertEqual(array(), $fields, t('Fields are empty'), t('Fields.'));

  // Valid field.
  $edit = array(
    'code_key' => 'test_field',
    'code_title' => 'Test field label',
    'code_code' => '<?php echo "test"; ?>',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields', $edit, t('Save code field'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertTrue(array_key_exists('test_field', $fields), t('test_field exists'), t('Fields'));
  $this
    ->assertEqual($fields['test_field']['title'], $edit['code_title'], t('Title equals Test field label'), t('Fields'));
  $this
    ->assertEqual($fields['test_field']['properties']['code'], $edit['code_code'], t('Code equals <?php echo "test"; ?>'), t('Fields'));

  // Try to add the same custom field, must fail.
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields', $edit, t('Save code field'));
  $this
    ->assertText(t('This field already exists.'), 'Key already exists', t('Fields.'));

  // Update field.
  $edit = array(
    'code_title' => 'Test field label 2',
    'code_code' => '<?php echo "test"; ?>',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields/edit/test_field', $edit, t('Save code field'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertEqual($fields['test_field']['title'], $edit['code_title'], t('Label equals Test field label 2'), t('Fields'));
  $this
    ->assertEqual(count($fields), 1, t('Only 1 field'), t('Custom fields'));

  // Add new field.
  $edit = array(
    'code_key' => 'test_field_two',
    'code_title' => 'Test label 3',
    'code_code' => 'My code',
    'code_exclude[page]' => TRUE,
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields', $edit, t('Save code field'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertEqual($fields['test_field_two']['title'], $edit['code_title'], t('Title equals Test label 3'), t('Fields'));
  $this
    ->assertEqual(count($fields), 2, t('2 custom fields found'), t('Custom fields'));
  $this
    ->drupalGet(DS_PATH_LAYOUT . '/page');
  $this
    ->assertNoRaw('Test label 3', t('Custom field excluded on page node type'), t('Custom fields'));
  $this
    ->drupalGet(DS_PATH_LAYOUT . '/story');
  $this
    ->assertRaw('Test label 3', t('Custom field available on story node type'), t('Custom fields'));

  // Delete field.
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields/delete/test_field_two', array(), t('Delete'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertFalse(array_key_exists('test_field_two', $fields), t('test_field_two removed'), t('Fields'));
  $this
    ->assertEqual(count($fields), 1, t('Only 1 field'), t('Custom fields'));

  // Override field.
  $edit = array(
    'code_title' => 'Override read more',
    'code_code' => '<?php echo "test"; ?>',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields/edit/read_more', $edit, t('Save code field'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertEqual($fields['read_more']['status'], DS_FIELD_STATUS_OVERRIDDEN, t('Read more is overridden'), t('Fields'));
  $this
    ->assertEqual($fields['read_more']['title'], $edit['code_title'], t('Label equals Override read more'), t('Fields'));
  $this
    ->assertEqual(count($fields), 2, t('2 fields'), t('Custom fields'));

  // Reset overridden field.
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields/delete/read_more', array(), t('Reset'));
  $fields = variable_get('nd_fields', array());
  $this
    ->assertFalse(array_key_exists('read_more', $fields), t('read_more removed'), t('Fields'));
  $this
    ->assertEqual(count($fields), 1, t('Only 1 field'), t('Custom fields'));

  // Invalid key.
  $edit = array(
    'code_key' => 'test_key moehaha',
    'code_title' => 'Test label',
    'code_code' => 'test code',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields', $edit, t('Save code field'));
  $this
    ->assertText(t('The machine-readable name must contain only lowercase letters, numbers and underscores.'), 'Key is not valid', t('Fields'));

  // Add new field and test with real code :)
  $edit = array(
    'code_key' => 'test_field_real',
    'code_title' => 'Real field',
    'code_code' => '<?php echo "Day: ". date("d", $object->created); ?>',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields', $edit, t('Save code field'));

  // Create page.
  $page_node = $this
    ->drupalCreateNode();

  // Let's do another one..
  $edit = array(
    'title[full][region]' => 'header',
    'test_field_real[full][region]' => 'middle',
  );
  $this
    ->drupalPost(DS_PATH_LAYOUT . '/page/full', $edit, t('Save'));

  // See if it's rendered.
  $this
    ->drupalGet('node/' . $page_node->nid);
  $this
    ->assertText('Day: ' . date("d", $page_node->created), t('Custom field executed.'), t('Fields'));
  $this
    ->assertRaw('field-test-field-real', t('Custom field uses default class'), t('Custom fields'));

  // Change same field and see if it's changed (cached fields clear test);
  $edit = array(
    'code_title' => 'Real field',
    'code_code' => '<?php echo "Day month: ". date("d M", $object->created); ?>',
    'code_class' => 'custom-class',
  );
  $this
    ->drupalPost(DS_PATH_MODULES . '/nd/fields/edit/test_field_real', $edit, t('Save code field'));
  $this
    ->drupalGet('node/' . $page_node->nid);
  $this
    ->assertText('Day month: ' . date("d M", $page_node->created), t('Custom field executed and changed (cached fields is cleared).'), t('Fields'));
  $this
    ->assertRaw('custom-class', t('Custom field uses default class'), t('Custom fields'));

  // Test with token module (if available).
  $token = drupal_get_filename('module', 'token');
  if (!empty($token)) {
    module_enable(array(
      'token',
    ));
    $edit = array(
      'code_title' => 'Real field',
      'code_code' => 'Day: [d]',
      'code_token' => FALSE,
    );
    $this
      ->drupalPost(DS_PATH_MODULES . '/nd/fields/edit/test_field_real', $edit, t('Save code field'));
    $fields = variable_get('nd_fields', array());
    $this
      ->drupalGet('node/' . $page_node->nid);
    $this
      ->assertNoText('Day: ' . date("j", $page_node->created), t('Custom field not executed with token support.'), t('Fields'));
    $edit = array(
      'code_title' => 'Real field',
      'code_code' => 'Day: [d]',
      'code_token' => TRUE,
    );
    $this
      ->drupalPost(DS_PATH_MODULES . '/nd/fields/edit/test_field_real', $edit, t('Save code field'));
    $fields = variable_get('nd_fields', array());
    $this
      ->drupalGet('node/' . $page_node->nid);
    $this
      ->assertText('Day: ' . date("j", $page_node->created), t('Custom field executed with token support.'), t('Fields'));
  }
}