<?php

/**
 * @file
 * Contains tests for views_handler_field.
 */

/**
 * Tests the generic field handler
 *
 * @see views_handler_field
 */
class ViewsHandlerFieldTest extends ViewsSqlTest {
  public static function getInfo() {
    return array(
      'name' => 'Field',
      'description' => 'Test the core views_handler_field handler.',
      'group' => 'Views Handlers',
    );
  }

  protected function setUp() {
    parent::setUp();
    $this->column_map = array(
      'views_test_name' => 'name',
    );
  }

  function testEmpty() {
    $this->_testHideIfEmpty();
    $this->_testEmptyText();
  }

  /**
   * Tests the hide if empty functionality.
   *
   * This tests alters the result to get easier and less coupled results.
   */
  function _testHideIfEmpty() {
    $view = $this->getBasicView();
    $view->init_display();
    $view->pre_execute();
    $view->execute();

    $column_map_reversed = array_flip($this->column_map);
    $view->row_index = 0;

    $view->field['name']->options['hide_empty'] = TRUE;
    $view->result[0]->{$column_map_reversed['name']} = "";
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, "", 'If hide_empty is checked, "" should be treated as empty.');

    $view->field['name']->options['empty_zero'] = FALSE;
    $view->result[0]->{$column_map_reversed['name']} = "0";
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, "0", 'If hide_empty is checked, but not empty_zero, "0" should be treated as not empty.');

    $view->field['name']->options['empty_zero'] = TRUE;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, "", 'If hide_empty and empty_zero are checked, "0" should be treated as empty.');

    $view->field['name']->options['hide_alter_empty'] = FALSE;
    $view->field['name']->options['alter']['alter_text'] = TRUE;
    $view->result[0]->{$column_map_reversed['name']} = "";
    $random_name = $this->randomName();
    $view->field['name']->options['alter']['text'] = $random_name;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $random_name, 'If hide_empty but not hide_alter_empty is checked, some rewrite should appear even if the value is empty.');

    $view->field['name']->options['hide_alter_empty'] = TRUE;
    $random_name = $this->randomName();
    $view->field['name']->options['alter']['text'] = $random_name;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, "", 'If hide_empty and hide_alter_empty are checked, rewrite should be empty all the time.');
  }

  /**
   * Tests the usage of the empty text.
   */
  function _testEmptyText() {
    $view = $this->getBasicView();
    $view->init_display();
    $view->pre_execute();
    $view->execute();

    $column_map_reversed = array_flip($this->column_map);
    $view->row_index = 0;

    $empty_text = $view->field['name']->options['empty'] = $this->randomName();
    $view->result[0]->{$column_map_reversed['name']} = "";
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $empty_text, 'If a field is empty, the empty text should be used for the output.');

    $view->result[0]->{$column_map_reversed['name']} = "0";
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, "0", 'If a field is 0 and empty_zero is not checked, the empty text should not be used for the output.');

    $view->result[0]->{$column_map_reversed['name']} = "0";
    $view->field['name']->options['empty_zero'] = TRUE;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $empty_text, 'If a field is 0 and empty_zero is checked, the empty text should be used for the output.');

    $view->result[0]->{$column_map_reversed['name']} = "";
    $view->field['name']->options['alter']['alter_text'] = TRUE;
    $alter_text = $view->field['name']->options['alter']['text'] = $this->randomName();
    $view->field['name']->options['hide_alter_empty'] = FALSE;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $alter_text, 'If a field is empty, some rewrite text exists, but hide_alter_empty is not checked, render the rewrite text.');

    $view->field['name']->options['hide_alter_empty'] = TRUE;
    $render = $view->field['name']->advanced_render($view->result[0]);
    $this->assertIdentical($render, $empty_text, 'If a field is empty, some rewrite text exists, and hide_alter_empty is checked, use the empty text.');
  }

}