Index: modules/tracker/tracker.test =================================================================== RCS file: /cvs/drupal/drupal/modules/tracker/tracker.test,v retrieving revision 1.12 diff -u -r1.12 tracker.test --- modules/tracker/tracker.test 11 Oct 2009 03:07:20 -0000 1.12 +++ modules/tracker/tracker.test 23 Nov 2009 05:23:17 -0000 @@ -21,6 +21,13 @@ $this->user = $this->drupalCreateUser($permissions); $this->other_user = $this->drupalCreateUser($permissions); + // Remove the "recent content" block because it will mess up our assertions + // later on if enabled. + db_delete('block') + ->condition('module', 'node') + ->condition('delta', 'recent') + ->execute(); + // Make node preview optional. variable_set('comment_preview_page', 0); } Index: profiles/default/default.install =================================================================== RCS file: /cvs/drupal/drupal/profiles/default/default.install,v retrieving revision 1.17 diff -u -r1.17 default.install --- profiles/default/default.install 6 Nov 2009 02:38:08 -0000 1.17 +++ profiles/default/default.install 23 Nov 2009 05:23:17 -0000 @@ -130,6 +130,16 @@ 'pages' => '', 'cache' => -1, ), + array( + 'module' => 'node', + 'delta' => 'recent', + 'theme' => 'garland', + 'status' => 1, + 'weight' => 0, + 'region' => 'sidebar_first', + 'pages' => '', + 'cache' => -1, + ), ); $query = db_insert('block')->fields(array('module', 'delta', 'theme', 'status', 'weight', 'region', 'pages', 'cache')); foreach ($values as $record) { Index: modules/node/node.module =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.module,v retrieving revision 1.1166 diff -u -r1.1166 node.module --- modules/node/node.module 8 Nov 2009 10:02:41 -0000 1.1166 +++ modules/node/node.module 23 Nov 2009 05:23:17 -0000 @@ -1975,6 +1975,7 @@ $blocks['syndicate']['info'] = t('Syndicate'); // Not worth caching. $blocks['syndicate']['cache'] = DRUPAL_NO_CACHE; + $blocks['recent']['info'] = t('Recent content'); return $blocks; } @@ -1982,13 +1983,68 @@ * Implement hook_block_view(). */ function node_block_view($delta = '') { - $block['subject'] = t('Syndicate'); - $block['content'] = theme('feed_icon', array('url' => url('rss.xml'), 'title' => t('Syndicate'))); + $block = array(); + switch ($delta) { + case 'syndicate': + $block['subject'] = t('Syndicate'); + $block['content'] = theme('feed_icon', array('url' => url('rss.xml'), 'title' => t('Syndicate'))); + break; + + case 'recent': + if (user_access('access content')) { + $block['subject'] = t('Recent content'); + $items = array(); + $select = db_select('node', 'n') + ->fields('n', array('uid', 'nid', 'title')) + ->condition('status', 1) + ->orderBy('created', 'DESC') + ->range(0, variable_get('node_recent_block_count', 10)) + ->addTag('node_access') + ->execute(); + while ($node = $select->fetch(PDO::FETCH_ASSOC)) { + $item = l($node['title'], 'node/' . $node['nid']); + $item .= '
'; + $item .= theme('username', array('account' => user_load($node['uid']))); + $items[] = $item; + } + if (count($items)) { + $block['content'] = theme('item_list', array('items' => $items)); + } + } + break; + + } return $block; } /** + * Implementation of hook_block_configure(). + */ +function node_block_configure($delta = '') { + $form = array(); + if ($delta == 'recent') { + $form['node_recent_block_count'] = array( + '#type' => 'select', + '#title' => t('Amount of recent content'), + '#default_value' => variable_get('node_recent_block_count', 10), + '#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)), + '#description' => t('Pieces of content displayed in the Recently posted block.'), + ); + } + return $form; +} + +/** + * Implementation of hook_block_save(). + */ +function node_block_save($delta = '', $edit = array()) { + if ($delta == 'recent') { + variable_set('node_recent_block_count', $edit['node_recent_block_count']); + } +} + +/** * A generic function for generating RSS feeds from a set of nodes. * * @param $nids Index: modules/node/node.test =================================================================== RCS file: /cvs/drupal/drupal/modules/node/node.test,v retrieving revision 1.54 diff -u -r1.54 node.test --- modules/node/node.test 8 Nov 2009 12:30:35 -0000 1.54 +++ modules/node/node.test 23 Nov 2009 05:23:17 -0000 @@ -18,6 +18,11 @@ parent::setUp(); $web_user = $this->drupalCreateUser(array('create article content', 'create page content')); $this->drupalLogin($web_user); + // Disable the "recent content" block, it will mess up our assertions if enabled. + db_delete('block') + ->condition('module', 'node') + ->condition('delta', 'recent') + ->execute(); } /** @@ -1073,3 +1078,116 @@ $this->assertTrue(strpos($output, 'Drupal is a registered trademark of Dries Buytaert.') !== FALSE); } } + +class RecentContentBlockTestCase extends DrupalWebTestCase { + protected $nodes; + protected $count = 10; + + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Recent content block'), + 'description' => t('Test to make sure the recent content block works properly; it makes sure that nodes appear there, in the correct order, and the settings work properly.'), + 'group' => t('Node'), + ); + } + + function setUp() { + // Clear the static $nodes cache. + $this->nodes = array(); + parent::setUp(); + } + + /** + * Test the recent content block with a few nodes, less than the maximum. + */ + function testRecentContentBlock() { + $this->createTestNodes(3); + $this->assertRecentContent(); + } + + /** + * Test the recent content block with a lot of nodes, more than the maximum. + */ + function testRecentContentBlockOverflow() { + $this->createTestNodes(18); + $this->assertRecentContent(); + } + + /** + * Test the recent content block with a non-standard maximum number of nodes. + */ + function testRecentContentBlockSettings() { + $this->setNodeRecentBlockCount(20); + $this->createTestNodes(18); + $this->assertRecentContent(); + } + + /** + * Test the recent content block with a non-standard maximum number of nodes, + * and have more nodes than that custom maximum. + */ + function testRecentContentBlockSettingsOverflow() { + $this->setNodeRecentBlockCount(5); + $this->createTestNodes(18); + $this->assertRecentContent(); + } + + /** + * Create a number of nodes, and store them in the protected $nodes array. + * + * @param $count + * The number of nodes to create. + */ + protected function createTestNodes($count) { + $time = REQUEST_TIME - 600; + for ($i = 0; $i < $count; $i++) { + $this->nodes[($count - $i) - 1] = $this->drupalCreateNode(array('created' => REQUEST_TIME + $i)); + } + $this->nodes[-1] = $this->drupalCreateNode(array('create' => REQUEST_TIME, 'status' => 0)); + } + + /** + * Helper function to set the maximum recent nodes shown on the block through + * the user interface. + */ + function setNodeRecentBlockCount($count) { + $test_user = $this->drupalCreateUser(array('administer blocks')); + $this->drupalLogin($test_user); + $this->drupalPost('admin/structure/block/manage/node/recent/configure', array('node_recent_block_count' => $count), t('Save block')); + $this->count = $count; + $this->drupalLogout(); + } + + /** + * Helper assertion that makes sure the nodes appear on the block correctly. + */ + protected function assertRecentContent() { + $this->refreshVariables(); + $count = $this->count; + $this->drupalGet(''); + $results = $this->elements->xpath('//div[@id="block-node-recent"]'); + $div = $results[0]; + if (!count($this->nodes)) { + $this->assertFalse($div, t('Block does not appear when no nodes exist.')); + return; + } + $this->assertTrue($div, t('Block appears when several nodes exist.')); + $title = $div->h2; + $this->assertEqual($title, t('Recent content'), t('Title of the block matches correctly.')); + // Under the block div, we have the
, and then another + // div surrounding the themed item list before we get to the ul. + $ul = $div->div->div->ul; + $this->assertEqual(count($ul->li), min(count($this->nodes) - 1, $count), t('The correct number of nodes are shown.')); + for ($i = 0; $i < $count; $i++) { + if (isset($this->nodes[$i])) { + $list_item = $ul->li[$i]; + $link = $list_item->a; + $this->assertEqual($link, $this->nodes[$i]->title[FIELD_LANGUAGE_NONE][0]['value'], t('The correct node title is shown.')); + } + } + $this->assertNoText($this->nodes[-1]->title[FIELD_LANGUAGE_NONE][0]['value'], t('Unpublished nodes not shown')); + } +} Index: modules/poll/poll.test =================================================================== RCS file: /cvs/drupal/drupal/modules/poll/poll.test,v retrieving revision 1.25 diff -u -r1.25 poll.test --- modules/poll/poll.test 16 Oct 2009 23:48:37 -0000 1.25 +++ modules/poll/poll.test 23 Nov 2009 05:23:17 -0000 @@ -252,6 +252,12 @@ function setUp() { parent::setUp('poll'); + // Remove the "recent content" block because it will mess up our assertions + // later if enabled. + db_delete('block') + ->condition('module', 'node') + ->condition('delta', 'recent') + ->execute(); // Create and login user $admin_user = $this->drupalCreateUser(array('administer blocks')); Index: modules/path/path.test =================================================================== RCS file: /cvs/drupal/drupal/modules/path/path.test,v retrieving revision 1.26 diff -u -r1.26 path.test --- modules/path/path.test 8 Nov 2009 11:19:02 -0000 1.26 +++ modules/path/path.test 23 Nov 2009 05:23:17 -0000 @@ -17,6 +17,12 @@ function setUp() { parent::setUp('path'); + // Remove the "recent content" block because it will mess up our assertions + // later if enabled. + db_delete('block') + ->condition('module', 'node') + ->condition('delta', 'recent') + ->execute(); // Create test user and login. $web_user = $this->drupalCreateUser(array('create page content', 'edit own page content', 'administer url aliases', 'create url aliases')); Index: modules/menu/menu.test =================================================================== RCS file: /cvs/drupal/drupal/modules/menu/menu.test,v retrieving revision 1.25 diff -u -r1.25 menu.test --- modules/menu/menu.test 6 Nov 2009 03:17:59 -0000 1.25 +++ modules/menu/menu.test 23 Nov 2009 05:23:16 -0000 @@ -526,6 +526,12 @@ function setUp() { parent::setUp('menu'); + // Remove the "recent content" block because it will mess up our assertions + // later if enabled. + db_delete('block') + ->condition('module', 'node') + ->condition('delta', 'recent') + ->execute(); $this->admin_user = $this->drupalCreateUser(array( 'access administration pages',