You are here

nodesinblock.test in Nodes In Block 6

Same filename and directory in other branches
  1. 7 nodesinblock.test

Tests for Nodes in block

File

nodesinblock.test
View source
<?php

/**
 * @file
 * Tests for Nodes in block
 */
class NodesInBlock extends DrupalWebTestCase {

  /**
   * Implementation of getInfo().
   */
  function getInfo() {
    return array(
      'name' => t('Nodes in block functionality'),
      'description' => t('Test nodes in block functionality.'),
      'group' => t('Nodes in block'),
    );
  }

  /**
   * Implementation of setUp().
   */
  function setUp() {
    parent::setUp('nodesinblock');
  }

  /**
   * Debug helper function. Writes values away to a text file in the files directory.
   */
  function _debugHelper($value, $writetype = 'a+') {
    $debug = fopen($this->originalFileDirectory . '/simpletestdebug.txt', 'a+');
    fwrite($debug, print_r($value, TRUE) . "\n");
    fclose($debug);
  }

  /**
   * Helper function to set configuration.
   */
  function _setNodesinblockConfiguration() {
    $variables = array(
      'nodesinblock_contenttypes' => 'a:2:{s:4:"page";s:4:"page";s:5:"story";s:5:"story";}',
      'nodesinblock_friendlyname_0' => 's:16:"Nodes in block 1";',
      'nodesinblock_friendlyname_1' => 's:16:"Nodes in block 2";',
      'nodesinblock_friendlyname_2' => 's:16:"Nodes in block 3";',
      'nodesinblock_visibility_0' => 's:1:"1";',
      'nodesinblock_visibility_1' => 's:1:"1";',
      'nodesinblock_visibility_2' => 's:1:"1";',
      'nodesinblock_page_block' => 'a:3:{i:1;i:1;i:2;i:2;i:3;i:0;}',
      'nodesinblock_page_collapsed' => 'i:1;',
      'nodesinblock_page_collapsible' => 'i:1;',
      'nodesinblock_page_label' => 's:14:"Nodes in block";',
      'nodesinblock_page_render' => 's:1:"0";',
      'nodesinblock_story_block' => 'a:3:{i:2;i:2;i:3;i:3;i:1;i:0;}',
      'nodesinblock_story_collapsed' => 'i:1;',
      'nodesinblock_story_collapsible' => 'i:1;',
      'nodesinblock_story_label' => 's:14:"Nodes in block";',
      'nodesinblock_story_render' => 's:1:"0";',
    );
    foreach ($variables as $key => $value) {
      db_query("INSERT INTO {variable} (name, value) VALUES ('%s', '%s')", $key, $value);
    }
    variable_set('nodesinblock_nrofblocks', '3');
    $this
      ->drupalGet('admin/build/block');
    db_query("UPDATE {blocks} set status = 1, region = 'right' where delta = 0 AND module = 'nodesinblock'");
    db_query("UPDATE {blocks} set status = 1, region = 'right' where delta = 1 AND module = 'nodesinblock'");
    db_query("UPDATE {blocks} set status = 1, region = 'right' where delta = 2 AND module = 'nodesinblock'");
  }

  /**
   * Helper function to create a node with standard settings.
   */
  function _createNode($type, $nid = 1, $region = 0, $vis = '*') {
    $edit = array(
      'title' => 'Title ' . $nid,
      'body' => 'Text ' . $nid,
      'nodesinblock_delta' => $region,
      'nodesinblock_visibility' => $vis,
      'nodesinblock_render' => NIB_RENDER_PAGE_WITH_LINKS,
    );
    $this
      ->drupalPost('node/add/' . $type, $edit, t('Save'));
    $result = db_result(db_query("SELECT nid FROM {nodesinblock} WHERE nid = %d", $nid));
    $this
      ->assertTrue($result, 'Node ' . $nid . ' saved in nodesinblock table', 'Node submission');
  }

  /**
   * Helper function to update an existing node.
   */
  function _updateNode($nid = 1, $edit = array()) {
    $this
      ->drupalPost('node/' . $nid . '/edit', $edit, t('Save'));
    $result = db_result(db_query("SELECT nid FROM {nodesinblock} WHERE nid = %d", $nid));
    $this
      ->assertTrue($result, 'Node ' . $nid . ' updated in nodesinblock table', 'Node submission');
  }

  /**
   * Helper function to get visibility from block.
   */
  function _getVisibility($delta) {
    return db_result(db_query("SELECT pages from {blocks} WHERE module = 'nodesinblock' AND delta = %d", $delta));
  }

  /**
   * Test submitting of nodes and see if they appear
   * in blocks and how they are rendered.
   */
  function testNodeSubmissions() {
    $admin_user = $this
      ->drupalCreateUser(array(
      'administer blocks',
      'administer nodes',
      'administer nodes in block configuration',
      'administer nodes in block queue',
    ));
    $this
      ->drupalLogin($admin_user);
    $this
      ->_setNodesinblockConfiguration();

    // Created a first node.
    $this
      ->_createNode('page', 1);
    $this
      ->assertRaw('Nodes in block 1', 'Nodes in block 1 queue found');

    // Should show up everywhere.
    $this
      ->drupalGet('node');
    $this
      ->assertRaw('Text 1', 'Title 1 node found');
    $this
      ->drupalGet('node/add');
    $this
      ->assertRaw('Text 1', 'Title 1 node found');

    // Update node to show only up on frontpage.
    $this
      ->_updateNode('1', array(
      'nodesinblock_visibility' => '<front>',
    ));
    $this
      ->drupalGet('node/add');
    $this
      ->assertNoRaw('Text 1', 'Title 1 node not found');
    $this
      ->assertNoRaw('Nodes in block 1', 'Block nodes in block 1 not found');
    $this
      ->drupalGet('node');
    $this
      ->assertRaw('Text 1', 'Title 1 node found');
    $this
      ->assertRaw('Nodes in block 1', 'Block nodes in block 1 found');

    // Update node and change block to 2.
    $this
      ->_updateNode('1', array(
      'nodesinblock_delta' => '1',
      'nodesinblock_visibility' => '<front>',
    ));
    $this
      ->drupalget('node/1/nodesinblock');
    $this
      ->assertRaw('Nodes in block 2', 'Nodes in block 2 queue found');
    $this
      ->drupalGet('node');
    $this
      ->assertNoRaw('Nodes in block 1', 'Block nodes in block 1 not found');
    $this
      ->assertRaw('Nodes in block 2', 'Block nodes in block 2 found');
    $this
      ->assertRaw('Text 1', 'Title 1 node found');

    // Create second node.
    $this
      ->_createNode('page', '2');
    $this
      ->assertRaw('Nodes in block 1', 'Nodes in block 1 queue found');
    $this
      ->drupalGet('node/add');
    $this
      ->assertRaw('Nodes in block 1', 'Block nodes in block 1 found');
    $this
      ->assertRaw('Text 2', 'Title 2 node found');
    $this
      ->assertNoRaw('Text 1', 'Title 1 node not found');
    $this
      ->assertNoRaw('Nodes in block 2', 'Block nodes in block 2 not found');
    $this
      ->drupalGet('node');
    $this
      ->assertRaw('Nodes in block 2', 'Block nodes in block 2 found');
    $this
      ->assertRaw('Text 1', 'Title 1 node found');
    $this
      ->assertRaw('Nodes in block 1', 'Block nodes in block 1 found');
    $this
      ->assertRaw('Text 2', 'Title 2 node found');

    // Do some tests on visibility settings of blocks.
    $this
      ->_updateNode('1', array(
      'nodesinblock_delta' => '0',
      'nodesinblock_visibility' => '*',
    ));
    $this
      ->_updateNode('2', array(
      'nodesinblock_delta' => '0',
      'nodesinblock_visibility' => '<front>',
    ));
    $visib = $this
      ->_getVisibility('0');
    $visib = explode("\n", $visib);
    $this
      ->assertTrue(in_array('<front>', $visib, '&lt;front&gt; visibility found.'));
    $this
      ->assertTrue(in_array('*', $visib, '* visibility found.'));
    $this
      ->_updateNode('1', array(
      'nodesinblock_delta' => '0',
      'nodesinblock_visibility' => 'node/add',
    ));
    $this
      ->_updateNode('2', array(
      'nodesinblock_delta' => '0',
      'nodesinblock_visibility' => 'user',
    ));
    $visib = $this
      ->_getVisibility('0');
    $visib = explode("\n", $visib);
    $this
      ->assertFalse(in_array('<front>', $visib, '&lt;front&gt; visibility not found.'));
    $this
      ->assertFalse(in_array('*', $visib, '&lt;front&gt; visibility not found.'));
    $this
      ->assertTrue(in_array('node/add', $visib, 'node/add visibility found.'));
    $this
      ->assertTrue(in_array('user', $visib, 'user visibility found.'));

    // Delete node and see
    // 1: that record is gone in nodesinblock table.
    // 2: visibility of block is good too.
    node_delete('2');
    $result = db_result(db_query("SELECT nid FROM {nodesinblock} WHERE nid = 2"));
    $this
      ->assertFalse($result, 'Row has been deleted from nodesinblock table');
    $visib = $this
      ->_getVisibility('0');
    $visib = explode("\n", $visib);
    $this
      ->assertFalse(in_array('user', $visib, 'user visibility not found.'));
    $this
      ->assertTrue(in_array('node/add', $visib, 'node/add visibility found.'));
  }

}

Classes

Namesort descending Description
NodesInBlock @file Tests for Nodes in block