xmlsitemap_node.test 6.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php
// $Id$

/**
 * @file
 * Unit tests for the xmlsitemap_node module.
 */

class XMLSitemapNodeTestHelper extends XMLSitemapTestHelper {
  protected $admin_user;
  protected $normal_user;
  protected $nodes = array();

  function setUp() {
    parent::setUp('xmlsitemap', 'xmlsitemap_node', 'comment');
16
    $this->admin_user = $this->drupalCreateUser(array('administer nodes', 'bypass node access', 'administer content types', 'administer xmlsitemap'));
17
    $this->normal_user = $this->drupalCreateUser(array('create page content', 'edit any page content', 'access content', 'view own unpublished content'));
18
    variable_set('xmlsitemap_node_status_page', 1);
19 20 21 22 23 24 25 26
  }

  //function addNodes($count) {
  //  for ($i = count($this->nodes); $i < ($count + 1); $i++) {
  //    $this->nodes[$i] = $this->drupalCreateNode();
  //  }
  //}

27
  protected function assertNodeSitemapLinkVisible(stdClass $node) {
28
    $link = xmlsitemap_link_load('node', $node->nid);
29
    return $this->assertSitemapLinkVisible($link);
30 31
  }

32
  protected function assertNodeSitemapLinkNotVisible(stdClass $node) {
33
    $link = xmlsitemap_link_load('node', $node->nid);
34
    return $this->assertSitemapLinkNotVisible($link);
35 36
  }

37
  protected function assertNodeSitemapLinkValues(stdClass $node, array $values) {
38
    $link = xmlsitemap_link_load('node', $node->nid);
39 40 41 42
    if (!$link) {
      $this->fail(t('Could not load sitemap link for node @nid.', array('@nid' => $node->nid)));
    }
    else {
43
      $this->assertSitemapLinkValues($link, $values);
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    }
  }
}

//class XMLSitemapNodeUnitTest extends DrupalWebTestCase {
//  public static function getInfo() {
//    return array(
//      'name' => 'XML Sitemap node unit tests',
//      'description' => 'Unit tests for the XML Sitemap node module.',
//      'group' => 'XML Sitemap',
//    );
//  }
//
//  function setUp() {
//    parent::setUp('xmlsitemap', 'xmlsitemap_node');
//  }
//}

class XMLSitemapNodeFunctionalTest extends XMLSitemapNodeTestHelper {
  public static function getInfo() {
    return array(
      'name' => 'XML sitemap node functional tests',
      'description' => 'Interface tests for the XML sitemap node module.',
      'group' => 'XML sitemap',
    );
  }

  function testNodeSettings() {
72 73
    $body_field = 'body[' . LANGUAGE_NONE . '][0][value]';

74
    $node = $this->drupalCreateNode(array('status' => FALSE, 'uid' => $this->normal_user->uid));
75
    $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
76 77 78 79 80 81 82 83

    $this->drupalLogin($this->normal_user);
    $this->drupalGet('node/' . $node->nid . '/edit');
    $this->assertNoField('xmlsitemap[status]');
    $this->assertNoField('xmlsitemap[priority]');

    $edit = array(
      'title' => 'Test node title',
84
      $body_field => 'Test node body',
85 86
    );
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
87
    $this->assertText('Basic page Test node title has been updated.');
88
    $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
89 90 91 92 93 94 95 96 97 98 99 100

    $this->drupalLogin($this->admin_user);
    $this->drupalGet('node/' . $node->nid . '/edit');
    $this->assertField('xmlsitemap[status]');
    $this->assertField('xmlsitemap[priority]');

    $edit = array(
      'xmlsitemap[status]' => 0,
      'xmlsitemap[priority]' => 0.9,
      'status' => TRUE,
    );
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
101
    $this->assertText('Basic page Test node title has been updated.');
102
    $this->assertNodeSitemapLinkValues($node, array('access' => 1, 'status' => 0, 'priority' => 0.9, 'status_override' => 1, 'priority_override' => 1));
103 104 105 106 107 108 109

    $edit = array(
      'xmlsitemap[status]' => 'default',
      'xmlsitemap[priority]' => 'default',
      'status' => FALSE,
    );
    $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
110
    $this->assertText('Basic page Test node title has been updated.');
111
    $this->assertNodeSitemapLinkValues($node, array('access' => 0, 'status' => 1, 'priority' => 0.5, 'status_override' => 0, 'priority_override' => 0));
112 113 114 115 116 117 118 119 120
  }

  /**
   * Test the content type settings.
   */
  function testTypeSettings() {
    $this->drupalLogin($this->admin_user);

    $node_old = $this->drupalCreateNode();
121
    $this->assertNodeSitemapLinkValues($node_old, array('status' => 1, 'priority' => 0.5));
122 123

    $edit = array(
124
      'xmlsitemap_node_status' => 0,
125 126
      'xmlsitemap_node_priority' => '0.0',
    );
127 128
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this->assertText(t('The content type Basic page has been updated.'));
129 130

    $node = $this->drupalCreateNode();
131 132
    $this->assertNodeSitemapLinkValues($node, array('status' => 0, 'priority' => 0.0));
    $this->assertNodeSitemapLinkValues($node_old, array('status' => 0, 'priority' => 0.0));
133 134

    $edit = array(
135
      'xmlsitemap_node_status' => 1,
136 137
      'xmlsitemap_node_priority' => '0.5',
    );
138 139
    $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this->assertText(t('The content type Basic page has been updated.'));
140

141 142
    $this->assertNodeSitemapLinkValues($node, array('status' => 1, 'priority' => 0.5));
    $this->assertNodeSitemapLinkValues($node_old, array('status' => 1, 'priority' => 0.5));
143 144

    //$this->drupalPost('node/' . $node->nid . '/edit', array(), t('Save'));
145
    //$this->assertText(t('Basic page @title has been updated.', array('@title' => $node->title)));
146 147 148 149 150 151 152 153 154
  }

  /**
   * Test the import of old nodes via cron.
   */
  function testCron() {
    $this->drupalLogin($this->admin_user);

    $limit = 5;
155
    $this->drupalPost('admin/config/search/xmlsitemap', array('xmlsitemap_batch_limit' => $limit), t('Save configuration'));
156 157 158 159 160 161 162 163 164 165 166 167
    $this->assertText(t('The configuration options have been saved.'));

    $nodes = array();
    for ($i = 1; $i <= ($limit + 1); $i++) {
      $node = $this->drupalCreateNode();
      array_push($nodes, $node);
      // Need to delay by one second so the nodes don't all have the same
      // timestamp.
      sleep(1);
    }

    // Clear all the node link data so we can emulate 'old' nodes.
168 169 170
    db_delete('xmlsitemap')
      ->condition('type', 'node')
      ->execute();
171 172

    // Run cron to import old nodes.
173
    xmlsitemap_node_cron();
174 175 176 177 178

    for ($i = 1; $i <= ($limit + 1); $i++) {
      $node = array_pop($nodes);
      if ($i <= $limit) {
        // The first $limit nodes should be inserted.
179
        $this->assertNodeSitemapLinkValues($node, array('access' => 1, 'status' => 1, 'lastmod' => $node->changed));
180 181 182
      }
      else {
        // Any beyond $limit should not be in the sitemap.
183
        $this->assertNoSitemapLink(array('type' => 'node', 'id' => $node->nid));
184 185 186 187
      }
    }
  }
}