<?php
/**
 * @file
 * Tests for different parts of the contemplate system
 */

/**
 * Test menu links depending on user permissions.
 */
class ContemplateNodeTypeTemplating extends DrupalWebTestCase {
  protected $admin_user;
  protected $web_user;
  protected $node;
  protected $type_name;
  protected $page_node;
  protected $custom_node;
  protected $test_body;

  public static function getInfo() {
    return array(
      'name' => 'Contemplate Test',
      'description' => 'Content Template test',
      'group' => 'Contemplate',
    );
  }

  function setUp() {
    // Additionally enable contact module.
    parent::setUp('contemplate');
    $this->assertTrue(module_exists('contemplate'), t('Contemplate module enabled.'));

    // Create test user.
    $this->admin_user = $this->drupalCreateUser(array('access content', 'administer content types','administer templates'));

    $this->web_user = $this->drupalCreateUser(array('access content'));
    
    $this->drupalLogin($this->admin_user);

    // Create content type, with underscores.
    $this->test_body = 'Some body text '.$this->randomName(25);
    $this->type_name =  strtolower($this->randomName(8)) . '_' .'test';
    $type = $this->drupalCreateContentType(array('name' => $this->type_name, 'type' => $this->type_name));
    $this->type = $type->type;

    // create random type content
    $settings = array();
    $settings['uid'] = $this->admin_user->uid;
    $settings['type'] = $this->type_name;
    $settings['body'][LANGUAGE_NONE][0]['value'] = $this->test_body;
    $settings['comment'] = 0;
    $this->custom_node = $this->drupalCreateNode($settings);

    // create page content
    $settings = array();
    $settings['uid'] = $this->admin_user->uid;
    $settings['type'] = 'page';
    $settings['comment'] = 0;
    $settings['body'][LANGUAGE_NONE][0]['value'] = $this->test_body;
    $this->page_node = $this->drupalCreateNode($settings);


  }

  // inspect basic admin page interface
  function testUIinterface() {
    $this->drupalGet('admin/structure/types/templates');
    $this->assertText('edit template');
  }


  function testPageTemplate() {
    // interface is alive and kicking
    $this->drupalGet('admin/structure/types/manage/'.$this->type_name.'/template');
    $this->assertText(t('Affect teaser output'));
    $this->assertText(t('Leave this field blank to leave body unaffected'));

    $this->drupalGet('admin/structure/types/manage/page/template');
    $this->assertText(t('Affect teaser output'));
    $this->assertText(t('Leave this field blank to leave body unaffected'));

    // check the node works before and after templating body (standard page type)
    $this->drupalGet('node/'.$this->page_node->nid);
    $this->assertText($this->test_body);

    // test different content types
    $edit = array();
    $edit['body-enable'] = true;
    $this->drupalPost('admin/structure/types/manage/page/template', $edit, t('Save'));

    $testwrap="xyz123xyz";

    $edit["bodyfield"] = $testwrap.'<?php print $node->body[$node->language][0][\'safe_value\'] ?>'.$testwrap;
    $this->drupalPost('admin/structure/types/manage/page/template', $edit, t('Save'));
    $this->assertText('template saved.');
    
    // submit a template for body and load the node to see if it stuck
    // load that node and see if it has that content
    $this->drupalGet('node/'.$this->page_node->nid);
    $this->assertText(  $testwrap ); // see if extra stuff we add to the template appears
    $this->assertText( $this->test_body );
  }

  function testPageTemplateDelete() {
    $edit = array();
    $this->drupalPost('admin/structure/types/manage/page/template', $edit, t('Delete'));
    $this->assertText( 'template has been reset' ); 
  }


}




