diff --git a/classes/Member.php b/classes/Member.php index f1bdf48696..aa3607aded 100644 --- a/classes/Member.php +++ b/classes/Member.php @@ -236,6 +236,20 @@ public function offices($include_only = NULL, $ignore_committees = FALSE) { } + + public function getPolicyURL($args = array()) { + $base_url = $this->url(); + if (isset($args['policy_number'])) { + $url = $base_url . '/divisions?policy=' . $args['policy_number']; + } else if (isset($args['policy_set'])) { + $url = $base_url . '/votes?policy=' . $args['policy_set']; + } else { + $url = $base_url . '/votes'; + } + + return $url; + } + private function getOfficeObject($include_only, $ignore_committees, $row) { if (!$this->includeOffice($include_only, $row['to_date'])) { return null; diff --git a/tests/PostcodePageTest.php b/tests/PostcodePageTest.php new file mode 100644 index 0000000000..f1a4a3e6cf --- /dev/null +++ b/tests/PostcodePageTest.php @@ -0,0 +1,76 @@ +createMySQLXMLDataSet(dirname(__FILE__).'/_fixtures/postcodepage.xml'); + } + + private function fetch_postcode_page($postcode, $options = array()) { + $args = array('pc' => $postcode); + $args = array_merge($args, $options); + + return $this->base_fetch_page( + $args, + 'postcode', + 'index.php', + '/postcode/' + ); + } + + public function testNoPostcodeGivesError() + { + $page = $this->fetch_postcode_page(''); + $this->assertContains('Please supply a postcode!', $page); + } + + public function testRedirectsForWestminsterOnly() + { + $page = $this->fetch_postcode_page('SW1A 1AA'); + $this->assertContains('Location: /mp/2/test_current-mp/test_westminster_constituency', $page); + } + + public function testShowsOptionsForScottishPostcode() + { + $page = $this->fetch_postcode_page('PH6 2DB'); + $this->assertContains('That postcode has multiple results', $page); + $this->assertContains('Test2 Current-MP', $page); + $this->assertContains('Test Current-MSP', $page); + $this->assertContains('Test Current-Regional-MSP', $page); + } + + public function testShowsOptionsForNIPostcode() + { + $page = $this->fetch_postcode_page('BT1 1AA'); + $this->assertContains('That postcode has multiple results', $page); + $this->assertContains('Test3 Current-MP', $page); + $this->assertContains('Test Current-MLA', $page); + } + + public function testRedirectsWithPolicySet() + { + $page = $this->fetch_postcode_page('SW1A 1AA', array('policy_set' => 'social')); + $this->assertContains('Location: /mp/2/test_current-mp/test_westminster_constituency/votes?policy=social', $page); + } + + public function testRedirectsWithPolicySetForScottishPostcode() + { + $page = $this->fetch_postcode_page('PH6 2DB', array('policy_set' => 'social')); + $this->assertContains('Location: /mp/3/test2_current-mp/test_scottish_westminster_constituency/votes?policy=social', $page); + } + + public function testRedirectsWithPolicySetForNIPostcode() + { + $page = $this->fetch_postcode_page('BT1 1AA', array('policy_set' => 'social')); + $this->assertContains('Location: /mp/6/test3_current-mp/test_ni_westminster_constituency/votes?policy=social', $page); + } +} diff --git a/tests/_fixtures/api.xml b/tests/_fixtures/api.xml index 6d327fbe95..24cb34ef6a 100644 --- a/tests/_fixtures/api.xml +++ b/tests/_fixtures/api.xml @@ -182,6 +182,14 @@ + + SW1A 1AA + Cities of London and Westminster + + + BT17 0XD + Belfast West|Belfast West + diff --git a/tests/_fixtures/postcode.xml b/tests/_fixtures/postcode.xml index 830f5d8e50..afffdade16 100644 --- a/tests/_fixtures/postcode.xml +++ b/tests/_fixtures/postcode.xml @@ -59,6 +59,10 @@ + + SW1A 1AA + Cities of London and Westminster + diff --git a/tests/_fixtures/postcodepage.xml b/tests/_fixtures/postcodepage.xml new file mode 100644 index 0000000000..f7c582eba0 --- /dev/null +++ b/tests/_fixtures/postcodepage.xml @@ -0,0 +1,241 @@ + + + + + + + + + + + + + + 1 + Test Bill + http://example.com + unknown + 0 + 2013-14 + test_ + + + + + + + + + + + + + Test Westminster Constituency + test_constituency_key + Test Constituency Value + + + + + Test Scottish Westminster Constituency + 1 + 1983-00-00 + 9999-12-31 + 11 + + + Test NI Westminster Constituency + 1 + 1983-00-00 + 9999-12-31 + 12 + + + + + + + + + + + + + + + + + + + + + 1 + 1 + Test Westminster Constituency + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 2 + 2013-08-07 11:02:49 + + + 2 + 1 + Test Scottish Westminster Constituency + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 3 + 2013-08-07 11:02:49 + + + 3 + 4 + Test Holyrood Constituency + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 4 + 2013-08-07 11:02:49 + + + 4 + 4 + Test Scottish Region + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 5 + 2013-08-07 11:02:49 + + + 1 + 5 + Test NI Westminster Constituency + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 6 + 2013-08-07 11:02:49 + + + 6 + 3 + Test Stormont Constituency + + 2007-05-06 + 9999-12-31 + general_election + still_in_office + 7 + 2013-08-07 11:02:49 + + + + + Test + Current-MP + 2000-01-01 + 9999-12-31 + 2 + Mrs + + + Test2 + Current-MP + 2000-01-01 + 9999-12-31 + 3 + Mr + + + Test + Current-MSP + 2000-01-01 + 9999-12-31 + 4 + Mrs + + + Test + Current-Regional-MSP + 2000-01-01 + 9999-12-31 + 5 + Dr + + + Test3 + Current-MP + 2000-01-01 + 9999-12-31 + 6 + Miss + + + Test + Current-MLA + 2000-01-01 + 9999-12-31 + 7 + Mr + + + + + + + + + + + + + + + PH6 2DB + Test Scottish Westminster Constituency|Test Holyrood Constituency|Test Scottish Region + + + BT1 1AA + Test NI Westminster Constituency|Test Stormont Constituency + + + SW1A 1AA + Test Westminster Constituency + + + OX1 4LF + Another Westminster Constituency + + + + + + + + + + + + + + + + + + + + + + diff --git a/www/docs/mp/index.php b/www/docs/mp/index.php index 54679d3c8e..c6f429e4eb 100644 --- a/www/docs/mp/index.php +++ b/www/docs/mp/index.php @@ -494,6 +494,7 @@ $divisions = new MySociety\TheyWorkForYou\Divisions($MEMBER, $positions, $policiesList); if ( $policyID ) { + $data['policy'] = $policiesList->getPolicyDetails($policyID); $data['policydivisions'] = $divisions->getMemberDivisionsForPolicy($policyID); } else { $data['policydivisions'] = $divisions->getAllMemberDivisionsByPolicy(); diff --git a/www/docs/postcode/index.php b/www/docs/postcode/index.php index cbd00bbb4d..f65e5f2955 100644 --- a/www/docs/postcode/index.php +++ b/www/docs/postcode/index.php @@ -26,16 +26,19 @@ } $out = ''; $sidebars = array(); -if (isset($constituencies['SPE']) || isset($constituencies['SPC'])) { +$options = get_policy_array(); +// if there is a policy set or a policy number then assume we want the MP +// as those arguments only make sense for MPs +if (count($options) == 0 && (isset($constituencies['SPE']) || isset($constituencies['SPC']))) { $MEMBER = fetch_mp($pc, $constituencies); list($out, $sidebars) = pick_multiple($pc, $constituencies, 'SPE', 'MSP'); -} elseif (isset($constituencies['NIE'])) { +} elseif (count($options) == 0 && isset($constituencies['NIE'])) { $MEMBER = fetch_mp($pc, $constituencies); list($out, $sidebars) = pick_multiple($pc, $constituencies, 'NIE', 'MLA'); } else { # Just have an MP, redirect instantly to the canonical page $MEMBER = fetch_mp($pc, $constituencies, 1); - member_redirect($MEMBER); + member_redirect($MEMBER, $options); } $PAGE->page_start(); @@ -48,10 +51,27 @@ function postcode_error($error) { global $PAGE; + $hidden = get_policy_array(); + $title = ''; + if (count($hidden) > 0 ) { + $policies = new \MySociety\TheyWorkForYou\Policies(); + + if (isset($hidden['policy_number'])) { + $policies = $policies->getPolicies(); + if (isset($policies[$hidden['policy_number']])) { + $title = "Find out how your MP voted on " . $policies[$hidden['policy_number']] . "."; + } + } else if (isset($hidden['policy_set'])) { + $sets = $policies->getSetDescriptions(); + if (isset($sets[$hidden['policy_set']])) { + $title = "Find out how your MP voted on " . $sets[$hidden['policy_set']] . "."; + } + } + } $PAGE->page_start(); $PAGE->stripe_start(); $PAGE->error_message($error); - $PAGE->postcode_form(); + $PAGE->postcode_form($hidden, $title); $PAGE->stripe_end(); $PAGE->page_end(); exit; @@ -64,7 +84,7 @@ function fetch_mp($pc, $constituencies, $house=null) { $args['house'] = $house; } try { - $MEMBER = new MEMBER($args); + $MEMBER = new \MySociety\TheyWorkForYou\Member($args); } catch (MySociety\TheyWorkForYou\MemberException $e){ postcode_error($e->getMessage()); } @@ -160,10 +180,32 @@ function pick_multiple($pc, $areas, $area_type, $rep_type) { return array($out, $sidebar); } -function member_redirect(&$MEMBER) { +function member_redirect(&$MEMBER, $options = array()) { if ($MEMBER->valid) { - $url = $MEMBER->url(); - header("Location: $url"); + if (count($options) > 0 ) { + $url = $MEMBER->getPolicyURL($options); + } else { + $url = $MEMBER->url(); + } + // awfulness so we can test this works becuase we use the CLI + // in our test framework for page requests and the CLI doesn't + // print out headers + if (php_sapi_name() === 'cli') { + print("Location: $url"); + } else { + header("Location: $url"); + } exit; } } + +function get_policy_array() { + $policy_options = array(); + if ($policy_set = get_http_var('policy_set')) { + $policy_options['policy_set'] = $policy_set; + } else if ($policy_number = get_http_var('policy_number')) { + $policy_options['policy_number'] = $policy_number; + } + + return $policy_options; +} diff --git a/www/includes/easyparliament/page.php b/www/includes/easyparliament/page.php index 73cbea9f4e..cae89d808d 100644 --- a/www/includes/easyparliament/page.php +++ b/www/includes/easyparliament/page.php @@ -349,14 +349,23 @@ public function heading() { $this->heading_displayed = true; } - public function postcode_form() { + public function postcode_form($hidden = array(), $title = '') { // Used on the mp (and yourmp) pages. // And the userchangepc page. global $THEUSER; + if ($title == '') { + $title = 'Find out about your MP/MSPs/MLAs'; + } + echo '
'; - $this->block_start(array('id'=>'mp', 'title'=>'Find out about your MP/MSPs/MLAs')); + $this->block_start(array('id'=>'mp', 'title'=>$title)); echo '
'; + if (count($hidden) > 0) { + foreach ($hidden as $field => $value) { + echo(''); + } + } if ($THEUSER->postcode_is_set()) { $FORGETURL = new URL('userchangepc'); $FORGETURL->insert(array('forget'=>'t')); diff --git a/www/includes/easyparliament/templates/html/mp/divisions.php b/www/includes/easyparliament/templates/html/mp/divisions.php index 3895d98f0c..97e652ad1a 100644 --- a/www/includes/easyparliament/templates/html/mp/divisions.php +++ b/www/includes/easyparliament/templates/html/mp/divisions.php @@ -126,8 +126,26 @@ + +
+

+

.

+ + + Photo: + + + + + + + + +
+ +
-

This person has not voted on this policy.

+

has not voted on this policy.