<?php 
/**
* @file
* Tests for the Entityform Block module
*/
class EntityFormBlockTestCase extends DrupalWebTestCase{
  public static function getInfo() {
    return array(
        'name' => 'Entityform Block module functionality',
        'description' => 'Tests Entityform Block module',
        'group' => 'Entityform block',
    );
  }
  
  public function setUp(){
    parent::setUp(array('entityform_block'));
    $this->admin_user = $this->drupalCreateUser(
            array('administer entityform types', 'administer entityforms', 'administer blocks'));
    $this->drupalLogin($this->admin_user);
        // Define the existing regions
    $this->region = 'sidebar_first';
//    $this->regions[] = 'header';
//    $this->regions[] = 'sidebar_first';
//    $this->regions[] = 'content';
//    $this->regions[] = 'sidebar_second';
//    $this->regions[] = 'footer';
    
    $this->entityFormType = strtolower($this->randomName(8));
    $this->entityFieldName = strtolower($this->randomName(8));

  }
  
  public function addEntityFormBlock() {
    $this->drupalGet('admin/structure/entityform_types/add');
    $this->assertText(t('Enable as a block'), "Option to enable a block appears on the entity form creation page");
    $this->assertNoFieldChecked('edit-data-block-set-enable-block', "Enable block is not checked.");
    
    $edit['label'] = $this->entityFormType;
    $edit['type'] = $this->entityFormType;
    $edit['data[block_set][enable_block]'] = TRUE;
    $this->drupalPost('admin/structure/entityform_types/add', $edit, t('Save entityform type'));
    $this->assertRaw(t('The Entityform @title has been created.', 
            array('@title' => $edit['label'])), 'Entitytype form has been created');
    $this->assertRaw(t('Entityform block @title has been created', 
            array('@title' => $edit['label'])), 'Entityform block has been created');
    $block_types = array();
    $block_types = variable_get('entityform_block_types');   
    
    $exists = entityform_block_exists($block_types, $this->entityFormType);
    $this->assertNotNull($exists, 'The entityform type block is in the variable');
    $delta = db_query("SELECT delta from {block} where module = :module and delta = :delta",
            array(':module' => 'entityform_block', ':delta' => $this->entityFormType))->fetchField();
    $this->assertNotNull($delta, t('Block found in the database'));
    
    // Add some fields.
    
    $field_edit = array(
        'fields[_add_new_field][field_name]' => $this->entityFieldName,
        'fields[_add_new_field][label]' => $this->entityFieldName,
        'fields[_add_new_field][type]' => 'text',
        'fields[_add_new_field][widget_type]' => 'text_textfield',
    );
    $url = 'admin/structure/entityform_types/manage/' . $this->entityFormType . '/fields';
    $this->drupalPost($url, $field_edit, t('Save'));
    $this->drupalPost(NULL, NULL, t('Save field settings'));
    $this->assertText(t('Updated field @type field settings.', array('@type' => $this->entityFieldName)));
    
  }
  
  public function assignEntityFormBlock() {
    // Enable block to some region.
    $region = 'sidebar_first';
    $block = array(
        'module' => 'entityform_block',
        'delta' => $this->entityFormType,
        'title' => $this->entityFormType,
    );
    $edit = array();
    $edit['blocks[' . $block['module'] . '_' . $block['delta'] . '][region]'] = $region;
    $this->drupalPost('admin/structure/block', $edit, t('Save blocks'));

    // Confirm that the block was moved to the proper region.
    $this->assertText(t('The block settings have been updated.'), 
            t('Block successfully moved to %region_name region.', array(
                '%region_name' => $region)));

    // Confirm that the block is being displayed.
    $this->drupalGet('node');
    $this->assertText(t($block['title']), t('Block successfully being displayed on the page.'));

    // Confirm that the custom block was found at the proper region.
    $xpath = $this->buildXPathQuery('//div[@class=:region-class]//div[@id=:block-id]/*', array(
      ':region-class' => 'region region-' . str_replace('_', '-', $region),
      ':block-id' => 'block-' . str_replace('_', '-', $block['module']) . '-' . $block['delta'],
    ));
    $this->assertFieldByXPath($xpath, NULL, t('Custom block found in %region_name region.', 
            array('%region_name' => $region)));
    // Check the form fields.
    $xpath_field = $this->buildXPathQuery('//div[@id=:field-id]/*', array(
        ':field-id' => 'edit-field-' . $this->entityFieldName,
        //':field-class' => 'field-type-text field-name-field-' . $this->entityFieldName . ' field-widget-text-textfield form-wrapper',
    ));
    $this->assertFieldByXPath($xpath_field, NULL, t('Custom form\'s fields found in form @title', 
            array('@title' => $this->entityFormType)));
    $this->assertFieldByName('field_' . $this->entityFieldName . '[und][0][value]', 
            '', t('Custom form\'s fields found in form @title', 
            array('@title' => $this->entityFormType)));
    // Test submission
    $this->drupalPost('node', array('field_' . $this->entityFieldName . '[und][0][value]' 
            => $this->randomName(8)), $submit);
    $arg = arg(2);
    $this->assertEqual($arg, 'confirm', 'The confirmation page has been loaded.');
  }
  
  public function disableEntityFormBlock () {
    $url = 'admin/structure/entityform_types/manage/' . $this->entityFormType;
    $this->drupalGet($url);
    $this->assertFieldChecked('edit-data-block-set-enable-block', "Enable block is checked.");
    $this->drupalGet('admin/structure/entityform_types/manage');
    $this->drupalPost($url, '', t('Save entityform type'));
    $edit = array();
    $edit['data[block_set][enable_block]'] = FALSE;
    $this->drupalPost($url, $edit, t('Save entityform type'));
    $this->assertRaw(t('Entityform block @title has been disabled.', array('@title' => $this->entityFormType)));
    $block_types = array();
    $exists = FALSE;
    $block_types = variable_get('entityform_block_types'); 
    if (!empty ($block_types)) {
      $exists = entityform_block_exists($block_types, $this->entityFormType);
    }
    $this->assertFalse($exists, 'The entityform block is not in the variable');
  }

  // TODO: permissions check
  public function testEntityFormBlock() {
    $this->addEntityFormBlock();
    $this->assignEntityFormBlock();
    $this->disableEntityFormBlock();
  }
  

}
    // @TODO:
    // Changing machine name updates the block name.
    // Delete the entityform type.