Projet

Général

Profil

Révision 42e6daf3

Ajouté par Julien Enselme il y a environ 10 ans

Update drupal 7.26 -> 7.27

Voir les différences:

drupal7/modules/simpletest/tests/form.test
1156 1156
      $this->assertText('State persisted.');
1157 1157
    }
1158 1158
  }
1159

  
1160
  /**
1161
   * Verify that the form build-id remains the same when validation errors
1162
   * occur on a mutable form.
1163
   */
1164
  function testMutableForm() {
1165
    // Request the form with 'cache' query parameter to enable form caching.
1166
    $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1)));
1167
    $buildIdFields = $this->xpath('//input[@name="form_build_id"]');
1168
    $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
1169
    $buildId = (string) $buildIdFields[0]['value'];
1170

  
1171
    // Trigger validation error by submitting an empty title.
1172
    $edit = array('title' => '');
1173
    $this->drupalPost(NULL, $edit, 'Continue submit');
1174

  
1175
    // Verify that the build-id did not change.
1176
    $this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails');
1177
  }
1178

  
1179
  /**
1180
   * Verifies that form build-id is regenerated when loading an immutable form
1181
   * from the cache.
1182
   */
1183
  function testImmutableForm() {
1184
    // Request the form with 'cache' query parameter to enable form caching.
1185
    $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1, 'immutable' => 1)));
1186
    $buildIdFields = $this->xpath('//input[@name="form_build_id"]');
1187
    $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
1188
    $buildId = (string) $buildIdFields[0]['value'];
1189

  
1190
    // Trigger validation error by submitting an empty title.
1191
    $edit = array('title' => '');
1192
    $this->drupalPost(NULL, $edit, 'Continue submit');
1193

  
1194
    // Verify that the build-id did change.
1195
    $this->assertNoFieldByName('form_build_id', $buildId, 'Build id changes when form validation fails');
1196

  
1197
    // Retrieve the new build-id.
1198
    $buildIdFields = $this->xpath('//input[@name="form_build_id"]');
1199
    $this->assertEqual(count($buildIdFields), 1, 'One form build id field on the page');
1200
    $buildId = (string) $buildIdFields[0]['value'];
1201

  
1202
    // Trigger validation error by again submitting an empty title.
1203
    $edit = array('title' => '');
1204
    $this->drupalPost(NULL, $edit, 'Continue submit');
1205

  
1206
    // Verify that the build-id does not change the second time.
1207
    $this->assertFieldByName('form_build_id', $buildId, 'Build id remains the same when form validation fails subsequently');
1208
  }
1209

  
1210
  /**
1211
   * Verify that existing contrib code cannot overwrite immutable form state.
1212
   */
1213
  public function testImmutableFormLegacyProtection() {
1214
    $this->drupalGet('form_test/form-storage', array('query' => array('cache' => 1, 'immutable' => 1)));
1215
    $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
1216
    $this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
1217
    $build_id = (string) $build_id_fields[0]['value'];
1218

  
1219
    // Try to poison the form cache.
1220
    $original = $this->drupalGetAJAX('form_test/form-storage-legacy/' . $build_id);
1221
    $this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
1222
    $this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
1223

  
1224
    // Assert that a watchdog message was logged by form_set_cache.
1225
    $status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, array(':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.'));
1226
    $this->assert($status, 'A watchdog message was logged by form_set_cache');
1227

  
1228
    // Ensure that the form state was not poisoned by the preceeding call.
1229
    $original = $this->drupalGetAJAX('form_test/form-storage-legacy/' . $build_id);
1230
    $this->assertEqual($original['form']['#build_id_old'], $build_id, 'Original build_id was recorded');
1231
    $this->assertNotEqual($original['form']['#build_id'], $build_id, 'New build_id was generated');
1232
    $this->assert(empty($original['form']['#poisoned']), 'Original form structure was preserved');
1233
    $this->assert(empty($original['form_state']['poisoned']), 'Original form state was preserved');
1234
  }
1235
}
1236

  
1237
/**
1238
 * Test the form storage when page caching for anonymous users is turned on.
1239
 */
1240
class FormsFormStoragePageCacheTestCase extends DrupalWebTestCase {
1241
  protected $profile = 'testing';
1242

  
1243
  public static function getInfo() {
1244
    return array(
1245
      'name'  => 'Forms using form storage on cached pages',
1246
      'description'  => 'Tests a form using form storage and makes sure validation and caching works when page caching for anonymous users is turned on.',
1247
      'group' => 'Form API',
1248
    );
1249
  }
1250

  
1251
  public function setUp() {
1252
    parent::setUp('form_test');
1253

  
1254
    variable_set('cache', TRUE);
1255
  }
1256

  
1257
  /**
1258
   * Return the build id of the current form.
1259
   */
1260
  protected function getFormBuildId() {
1261
    $build_id_fields = $this->xpath('//input[@name="form_build_id"]');
1262
    $this->assertEqual(count($build_id_fields), 1, 'One form build id field on the page');
1263
    return (string) $build_id_fields[0]['value'];
1264
  }
1265

  
1266
  /**
1267
   * Build-id is regenerated when validating cached form.
1268
   */
1269
  public function testValidateFormStorageOnCachedPage() {
1270
    $this->drupalGet('form_test/form-storage-page-cache');
1271
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
1272
    $this->assertText('No old build id', 'No old build id on the page');
1273
    $build_id_initial = $this->getFormBuildId();
1274

  
1275
    // Trigger validation error by submitting an empty title.
1276
    $edit = array('title' => '');
1277
    $this->drupalPost(NULL, $edit, 'Save');
1278
    $this->assertText($build_id_initial, 'Old build id on the page');
1279
    $build_id_first_validation = $this->getFormBuildId();
1280
    $this->assertNotEqual($build_id_initial, $build_id_first_validation, 'Build id changes when form validation fails');
1281

  
1282
    // Trigger validation error by again submitting an empty title.
1283
    $edit = array('title' => '');
1284
    $this->drupalPost(NULL, $edit, 'Save');
1285
    $this->assertText('No old build id', 'No old build id on the page');
1286
    $build_id_second_validation = $this->getFormBuildId();
1287
    $this->assertEqual($build_id_first_validation, $build_id_second_validation, 'Build id remains the same when form validation fails subsequently');
1288

  
1289
    // Repeat the test sequence but this time with a page loaded from the cache.
1290
    $this->drupalGet('form_test/form-storage-page-cache');
1291
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'HIT', 'Page was cached.');
1292
    $this->assertText('No old build id', 'No old build id on the page');
1293
    $build_id_from_cache_initial = $this->getFormBuildId();
1294
    $this->assertEqual($build_id_initial, $build_id_from_cache_initial, 'Build id is the same as on the first request');
1295

  
1296
    // Trigger validation error by submitting an empty title.
1297
    $edit = array('title' => '');
1298
    $this->drupalPost(NULL, $edit, 'Save');
1299
    $this->assertText($build_id_initial, 'Old build id is initial build id');
1300
    $build_id_from_cache_first_validation = $this->getFormBuildId();
1301
    $this->assertNotEqual($build_id_initial, $build_id_from_cache_first_validation, 'Build id changes when form validation fails');
1302
    $this->assertNotEqual($build_id_first_validation, $build_id_from_cache_first_validation, 'Build id from first user is not reused');
1303

  
1304
    // Trigger validation error by again submitting an empty title.
1305
    $edit = array('title' => '');
1306
    $this->drupalPost(NULL, $edit, 'Save');
1307
    $this->assertText('No old build id', 'No old build id on the page');
1308
    $build_id_from_cache_second_validation = $this->getFormBuildId();
1309
    $this->assertEqual($build_id_from_cache_first_validation, $build_id_from_cache_second_validation, 'Build id remains the same when form validation fails subsequently');
1310
  }
1311

  
1312
  /**
1313
   * Build-id is regenerated when rebuilding cached form.
1314
   */
1315
  public function testRebuildFormStorageOnCachedPage() {
1316
    $this->drupalGet('form_test/form-storage-page-cache');
1317
    $this->assertEqual($this->drupalGetHeader('X-Drupal-Cache'), 'MISS', 'Page was not cached.');
1318
    $this->assertText('No old build id', 'No old build id on the page');
1319
    $build_id_initial = $this->getFormBuildId();
1320

  
1321
    // Trigger rebuild, should regenerate build id.
1322
    $edit = array('title' => 'something');
1323
    $this->drupalPost(NULL, $edit, 'Rebuild');
1324
    $this->assertText($build_id_initial, 'Initial build id as old build id on the page');
1325
    $build_id_first_rebuild = $this->getFormBuildId();
1326
    $this->assertNotEqual($build_id_initial, $build_id_first_rebuild, 'Build id changes on first rebuild.');
1327

  
1328
    // Trigger subsequent rebuild, should regenerate the build id again.
1329
    $edit = array('title' => 'something');
1330
    $this->drupalPost(NULL, $edit, 'Rebuild');
1331
    $this->assertText($build_id_first_rebuild, 'First build id as old build id on the page');
1332
    $build_id_second_rebuild = $this->getFormBuildId();
1333
    $this->assertNotEqual($build_id_first_rebuild, $build_id_second_rebuild, 'Build id changes on second rebuild.');
1334
  }
1159 1335
}
1160 1336

  
1161 1337
/**

Formats disponibles : Unified diff