You are here

public function ClassifiedBasicTest::test1653560 in Classified Ads 6.3

Add server-side body length validation.

File

tests/classified_basic.test, line 648
Basic test for known bugs in previous versions.

Class

ClassifiedBasicTest

Code

public function test1653560() {
  $this->group = __FUNCTION__;
  $accounts = array(
    'creator',
  );
  $this
    ->createUsers($accounts);
  $this
    ->drupalLogin($this->creatorUser);

  // 1. Get the node type name.
  $type = node_get_types('name', 'classified');

  // 2. Get the Classified vocabulary id.
  $vid = _classified_get('vid');

  // 3a. Create a term in it, do not assign a specific lifetime.
  $term = array(
    'name' => $this
      ->randomName(8),
    'description' => $this
      ->randomString(20),
    'vid' => $vid,
  );
  $status = taxonomy_save_term($term);
  $tid = $term['tid'];
  $this
    ->assertEqual($status, SAVED_NEW, t('Term @tid created in default vocabulary.', array(
    '@tid' => $tid,
  )), 'setup');

  // 3b. Limit the maximum body length.
  $max_length = 20;
  variable_set('classified-max-length', $max_length);

  // 4a. Try to create an ad bearing that term, with a body below max length.
  $count = db_result(db_query('SELECT COUNT(nid) FROM {node}'));
  $this
    ->assertEqual(0, $count, t('No node found before creation of first ad'), $this->group);
  $edit = array(
    'title' => $this
      ->randomString(10),
    "taxonomy[{$vid}]" => 1,
    // Set by module
    'body' => 'A short body',
  );
  $ret = $this
    ->drupalPost('node/add/classified', $edit, t('Save'));
  $raw = t('@type %title has been created.', array(
    '@type' => $type,
    '%title' => $edit['title'],
  ));
  $this
    ->assertRaw($raw, t('Ad with proper body receives creation confirmation'), $this->group);
  $count = db_result(db_query('SELECT COUNT(nid) FROM {node}'));
  $this
    ->assertEqual(1, $count, t('Ad with proper body actually created.'), $this->group);

  // 4b. Try to create an ad bearing that term, with a body above max length.
  $edit = array(
    'title' => $this
      ->randomString(10),
    "taxonomy[{$vid}]" => 1,
    // Set by module
    'body' => 'A body longer than 20 characters.',
  );
  $ret = $this
    ->drupalPost('node/add/classified', $edit, t('Save'));
  $raw = t('Text is longer than maximum authorized length: @body_length characters vs @max_length authorized.', array(
    '@body_length' => drupal_strlen($edit['body']),
    '@max_length' => $max_length,
  ));
  $this
    ->assertRaw($raw, t('Ad with extra-long body receives post error.'), $this->group);
  $count = db_result(db_query('SELECT COUNT(nid) FROM {node}'));
  $this
    ->assertEqual(1, $count, t('Ad with extra-long body actually not created.'), $this->group);
}