Completed
Push — master ( a6ee27...1b5fd0 )
by Antony
47:13
created

KnockoutFormTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
/**
3
 * KnockoutFormTest
4
 *
5
 * Controller tests
6
 * 
7
 * @package framework
8
 * @subpackage tests
9
 */
10
class KnockoutFormTest extends FunctionalTest {
11
12
	public function setUp() {
13
		parent::setUp();
14
		Config::inst()->update('SSViewer', 'source_file_comments', true);
15
	}
16
17
	public function testKnockoutForm() {
18
		$page = $this->get('KnockoutFormTest_Controller');
19
		$this->assertEquals(200, $page->getStatusCode(), "a page should load");
0 ignored issues
show
Bug introduced by
The method assertEquals() does not seem to exist on object<KnockoutFormTest>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
20
		$body = $page->getBody();
21
		//SS_Log::log(new Exception(print_r($body, true)), SS_Log::NOTICE);
22
23
		$this->assertContains('data-bind="submit: addToCart"',$body,'form element has submit binding to javascript function');
24
		$this->assertContains('<input data-bind="textInput: spaceship, hasFocus: true"', $body, 'Databind attribute in input element');
25
		$this->assertContains("setKnockout:{value:'Enterprise\'s Voyage'}", $body, 'Comma escaped in HTML for javascript');
26
		$this->assertContains('<select data-bind="value: menu"', $body, 'Databind attribute applied to select element');
27
		$this->assertContains('<input data-bind="textInput: seatNumber, setKnockout:{value:4}"', $body, 'KnockoutNumericField works');
28
		
29
		$this->assertContains('<input data-bind="enable: canSaveInterGalacticAction, css:{ \'FormAction_Disabled\': !canSaveInterGalacticAction() }" type="submit"', $body, 'Databind attribute in submit button');
30
31
		$this->assertContains('<input data-bind="textInput: email, setKnockout:{value:\'[email protected]\'}"', $body, 'Databind attribute applied to input element for email field');
32
		$this->assertContains('class="email text"', $body, 'KnockoutEmailField has a class of "email text"');
33
34
		$this->assertContains('<textarea data-bind="textInput: comments"', $body, 'Databind attribute applied to the textareafield');
35
		$this->assertContains('class="knockouttextarea textarea"', $body, 'KnockoutTextareaField has a class of "textarea text"');
36
37
		$this->assertContains('data-bind="checked: accessories, setKnockout:{value:\'Zero Gravity Pillow\'}, blah: console.log(\'blast-off\')"', $body, 'Databind attribute applied to the radio buttons');
38
		$this->assertContains('class="radio"', $body, 'KnockoutOptionsetField has a class of "radio"');
39
		$this->assertContains('data-bind="enable: canSaveInterGalacticAction', $body, 'KnockoutFormAction has an obserable of "canSaveInterGalacticAction"');
40
41
42
43
	}
44
}
45
46
47
class KnockoutFormTest_Controller extends Controller implements TestOnly {
48
49
	private static $allowed_actions = array('Form');
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
50
51
	private static $url_handlers = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
52
		'$Action//$ID/$OtherID' => "handleAction",
53
	);
54
55
	protected $template = 'BlankPage';
56
	
57
	public function Link($action = null) {
58
		return Controller::join_links('KnockoutFormTest_Controller', $this->request->latestParam('Action'),
59
			$this->request->latestParam('ID'), $action);
60
	}
61
	
62
	public function Form() {
63
		$form = KnockoutForm::create(
64
			$this,
65
			'Form',
66
			FieldList::create(
67
				KnockoutTextField::create('Spaceship', 'Spaceship')
68
					->setObservable('spaceship')
69
					->setHasFocus(true),
70
				KnockoutTextField::create('FieldWithComma', 'FieldWithComma')
71
					->setObservable('fieldwithcomma')
72
					->setValue("Enterprise's Voyage"),
73
				KnockoutDropdownField::create('Menu', 'Space Menu', array('1'=>'Light Speed Salad','2'=>'Comet Custard'))
74
					->setObservable('menu'),
75
				KnockoutNumericField::create('SeatNumber', 'Seat Number', 4)
76
					->setObservable('seatNumber'),
77
				KnockoutEmailField::create('Email', 'Email')
78
					->setObservable('email')
79
					->setValue('[email protected]'),
80
				KnockoutTextareaField::create('Comments', 'Comments')
81
					->setObservable('comments'),
82
				KnockoutOptionsetField::create('Accessories', 'Accessories', array(
83
	            	'Flying High DVD' => 'Flying High DVD',
84
	            	'Zero Gravity Pillow' => 'Zero Gravity Pillow',
85
	            	'Rocket Replica' => 'Rocket Replica'
86
            	), 'Zero Gravity Pillow')
87
					->setObservable('accessories')
88
					->setOtherBindings("blah: console.log('blast-off')"),
89
				CheckboxSetField::create('Boxes', null, array('1'=>'one','2'=>'two'))
90
			),
91
			FieldList::create(
92
				KnockoutFormAction::create('doSubmit', 'Submit')
93
					->setObservable('canSaveInterGalacticAction')
94
			)
95
		);
96
		$form->setSubmit('addToCart');  // using KnockoutForm
97
98
		return $form;
99
	}
100
	
101
	public function doSubmit($data, $form, $request) {
0 ignored issues
show
Unused Code introduced by
The parameter $data is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
102
		$form->sessionMessage('Test save was successful', 'good');
103
		return $this->redirectBack();
104
	}
105
106
	public function getViewer($action = null) {
107
		return new SSViewer('BlankPage');
108
	}
109
110
}
111
112
113
114