浏览代码

added more tests

chris 11 年之前
父节点
当前提交
f7179b98b8
共有 2 个文件被更改,包括 30 次插入3 次删除
  1. 3 3
      Controller/Component/AjaxComponent.php
  2. 27 0
      Test/Case/Controller/Component/AjaxComponentTest.php

+ 3 - 3
Controller/Component/AjaxComponent.php

@@ -112,11 +112,11 @@ class AjaxComponent extends Component {
 
 		$this->Controller->autoRender = true;
 		$this->Controller->set('_redirect', compact('url', 'status', 'exit'));
-		$content = array();
+		$serializeKeys = array('_redirect');
 		if (!empty($this->Controller->viewVars['_serialize'])) {
-			$content = $this->Controller->viewVars['_serialize'];
+			$serializeKeys = array_merge($serializeKeys, $this->Controller->viewVars['_serialize']);
 		}
-		$this->Controller->set('_serialize', array_merge(array('_redirect'), $content));
+		$this->Controller->set('_serialize', $serializeKeys);
 
 		return false;
 	}

+ 27 - 0
Test/Case/Controller/Component/AjaxComponentTest.php

@@ -150,6 +150,33 @@ class AjaxComponentTest extends CakeTestCase {
 		$this->assertEquals('content', $this->Controller->viewVars['_serialize'][0]);
 	}
 
+	/**
+	 * AjaxComponentTest::testSetVarsWithRedirect()
+	 *
+	 * @return void
+	 */
+	public function testSetVarsWithRedirect() {
+		$_SERVER['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest';
+		$this->Controller->startupProcess();
+
+		$content = array('id' => 1, 'title' => 'title');
+		$this->Controller->set(compact('content'));
+		$this->Controller->set('_serialize', array('content'));
+
+		$this->Controller->redirect('/');
+		$this->assertSame(array(), $this->Controller->response->header());
+
+		$expected = array(
+			'url' => Router::url('/', true),
+			'status' => null,
+			'exit' => true
+		);
+		$this->assertEquals($expected, $this->Controller->viewVars['_redirect']);
+
+		$this->assertNotEmpty($this->Controller->viewVars);
+		$this->assertNotEmpty($this->Controller->viewVars['_serialize']);
+		$this->assertTrue(in_array('content', $this->Controller->viewVars['_serialize']));
+	}
 }
 
 // Use Controller instead of AppController to avoid conflicts