You are here

public function ClassifiedTestTestBasicTest::test1653560 in Classified Ads 7.3

Add server-side body length validation.

File

tests/classified_test_basic.test, line 535

Class

ClassifiedTestTestBasicTest
Basic test for known bugs in previous versions.

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_type_get_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 = (object) array(
    'name' => $this
      ->randomName(8),
    'description' => $this
      ->randomString(20),
    'vid' => $vid,
  );
  $status = taxonomy_term_save($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_query('SELECT COUNT(nid) FROM {node}')
    ->fetchField();
  $this
    ->assertEqual(0, $count, t('No node found before creation of first ad'), $this->group);
  $langcode = LANGUAGE_NONE;
  $body_key = "body[{$langcode}][0][value]";
  $term_key = "classified_category[{$langcode}]";
  $edit = array(
    'title' => $this
      ->randomString(10),
    $term_key => $tid,
    $body_key => 'A short body',
    'expire_mode' => 'reset',
  );
  $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_query('SELECT COUNT(nid) FROM {node}')
    ->fetchField();
  $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),
    $term_key => $tid,
    $body_key => 'A body longer than 20 characters',
  );
  $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_key]),
    '@max_length' => $max_length,
  ));
  $this
    ->assertRaw($raw, t('Ad with extra-long body receives post error.'), $this->group);
  $count = db_query('SELECT COUNT(nid) FROM {node}')
    ->fetchField();
  $this
    ->assertEqual(1, $count, t('Ad with extra-long body actually not created.'), $this->group);
}