<?php

/**
 * @file
 * Base functions and tests for Display Suite.
 */

class dsExportablesTests extends dsBaseTest {

  /**
   * Implements getInfo().
   */
  public static function getInfo() {
    return array(
      'name' => t('Exportables'),
      'description' => t('Tests for exportables in Display Suite.'),
      'group' => t('Display Suite'),
    );
  }

  /**
   * Enables the exportables module.
   */
  function dsExportablesSetup() {
    module_enable(array('ds_exportables_test'));
    drupal_flush_all_caches();
  }

  // Test view modes.
  function testDSExportablesViewmodes() {
    $this->dsExportablesSetup();

    // Find a default view mode on admin screen.
    $this->drupalGet('admin/structure/ds/view_modes');
    $this->assertText('Test exportables', t('Exportables view mode found on admin screen.'));

    // Find default view mode on layout screen.
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertText('Test exportables', t('Exportables view mode found on display screen.'));

    // Override default view mode.
    $edit = array(
      'name' => 'Testing 2',
    );
    $this->drupalPost('admin/structure/ds/view_modes/manage/test_exportables', $edit, t('Save'));
    $this->assertText(t('The view mode Testing 2 has been saved'), t('Exportables label updated'));
    $this->assertText(t('Revert'), t('Revert button found.'));

    // Find default view mode on layout screen.
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertText('Testing 2', t('Updated exportables view mode found on display screen.'));

    // Revert the view mode.
    $this->drupalPost('admin/structure/ds/view_modes/revert/test_exportables', array(), t('Revert'));
    $this->assertText(t('The view mode Testing 2 has been reverted'), t('Testing view mode reverted'));
    $this->assertText('Test exportables', t('Exportables view mode found on admin screen.'));

    // Assert the view mode is gone at the manage display screen.
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertNoText('Testing 2', t('Overrided exportables view mode not found on display screen.'));
    $this->assertText('Test exportables', t('Default exportables view mode found on display screen.'));
  }

  // Test layout and field settings configuration.
  function testDSExportablesLayoutFieldsettings() {
    $this->dsExportablesSetup();

    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertNoText(t('This layout is overridden. Click to revert to default settings.'));

    $settings = array(
      'type' => 'article',
      'title' => 'Exportable'
    );
    $node = $this->drupalCreateNode($settings);
    $this->drupalGet('node/' . $node->nid);
    $this->assertRaw('group-left', 'Left region found');
    $this->assertRaw('group-right', 'Right region found');
    $this->assertNoRaw('group-header', 'No header region found');
    $this->assertNoRaw('group-footer', 'No footer region found');
    $this->assertRaw('<h3><a href="'. url('node/1') . '" class="active">Exportable</a></h3>', t('Default title with h3 found'));
    $this->assertRaw('<a href="' . url('node/1') . '" class="active">Read more</a>', t('Default read more found'));

    // Override default layout.
    $layout = array(
      'additional_settings[layout]' => 'ds_2col_stacked',
    );

    $assert = array(
      'regions' => array(
        'header' => '<td colspan="8">' . t('Header') . '</td>',
        'left' => '<td colspan="8">' . t('Left') . '</td>',
        'right' => '<td colspan="8">' . t('Right') . '</td>',
        'footer' => '<td colspan="8">' . t('Footer') . '</td>',
      ),
    );

    $fields = array(
      'fields[post_date][region]' => 'header',
      'fields[author][region]' => 'left',
      'fields[links][region]' => 'left',
      'fields[body][region]' => 'right',
      'fields[comments][region]' => 'footer',
    );

    $this->dsSelectLayout($layout, $assert);
    $this->assertText(t('This layout is overridden. Click to revert to default settings.'));
    $this->dsConfigureUI($fields);

    $this->drupalGet('node/' . $node->nid);
    $this->assertRaw('group-left', 'Left region found');
    $this->assertRaw('group-right', 'Left region found');
    $this->assertRaw('group-header', 'Left region found');
    $this->assertRaw('group-footer', 'Left region found');

    // Revert.
    $edit = array();
    $this->drupalPost('admin/structure/ds/revert-layout/node|article|default', $edit, t('Revert'), array('query' => array('destination' => 'admin/structure/types/manage/article/display')));
    $this->drupalGet('node/' . $node->nid);
    $this->assertRaw('group-left', 'Left region found');
    $this->assertRaw('group-right', 'Left region found');
    $this->assertNoRaw('group-header', 'Left region found');
    $this->assertNoRaw('group-footer', 'Left region found');
    $this->assertRaw('<h3><a href="'. url('node/1') . '" class="active">Exportable</a></h3>', t('Default title with h3 found'));
    $this->assertRaw('<a href="' . url('node/1') . '" class="active">Read more</a>', t('Default read more found'));
  }

  // Test custom field exportables.
  function testDSExportablesCustomFields() {
    $this->dsExportablesSetup();

    // Look for default custom field.
    $this->drupalGet('admin/structure/ds/fields');
    $this->assertText('Exportable field');
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertText('Exportable field');

    // Override custom field.
    // Update testing label
    $edit = array(
      'name' => 'Overridden field',
    );
    $this->drupalPost('admin/structure/ds/fields/manage_custom/ds_exportable_field', $edit, t('Save'));
    $this->assertText(t('The field Overridden field has been saved'), t('Default exportable field label updated'));
    $this->assertText('Overridden field');
    $this->assertNoText('Exportable field');
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertText('Overridden field');
    $this->assertNoText('Exportable field');

    // Revert.
    $edit = array();
    $this->drupalPost('admin/structure/ds/fields/revert/ds_exportable_field', $edit, t('Revert'));
    $this->assertText('The field Overridden field has been reverted', t('Field reverted'));
    $this->assertText('Exportable field');
    $this->drupalGet('admin/structure/types/manage/article/display');
    $this->assertNoText('Overridden field');
    $this->assertText('Exportable field');
  }
}