diff --git a/tests/uc_addresses.views.test b/tests/uc_addresses.views.test index 1d276cf..6d9c2e6 100644 --- a/tests/uc_addresses.views.test +++ b/tests/uc_addresses.views.test @@ -73,6 +73,9 @@ class UcAddressesViewsTestCase extends UcAddressesTestCase { protected function UcAddressesCreateAddresses() { foreach ($this->accounts as $account) { $addressBook = UcAddressesAddressBook::get($account->uid); + // Clear address book cache to clear up leftover addresses + // from previous tests. + $addressBook->reset(); // Create default address. $address = $addressBook->addAddress(); $address->setAsDefault(); @@ -206,7 +209,125 @@ class UcAddressesViewsTestCase extends UcAddressesTestCase { } /** - * Creates a View. + * Tests if the view, edit and delete links are only + * displayed for users that are allowed to perform these tasks. + */ + public function testActionLinks() { + // Setup View. + $this->createActionLinksView(); + + $this->doActionLinksTests($this->accounts['customerBasic'], FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['customerViewDef'], TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['customerView'], TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['customerEdit'], TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['customerDelete'], TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['adminViewDef'], TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['adminView'], TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE); + $this->doActionLinksTests($this->accounts['adminEdit'], TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE); + $this->doActionLinksTests($this->accounts['adminAll'], TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE); + } + + /** + * Tests if the right actions links are displayed for one particular + * account. + * + * @param object $account + * The account to check access for. + * @param boolean $view_own_def + * If the user is supposed to see his own default addresses. + * @param boolean $view_own + * If the user is supposed to see all of his own addresses. + * @param boolean $edit_own + * If the user is supposed to edit all of his own addresses. + * @param boolean $delete_own + * If the user is supposed to delete all of his own addresses. + * @param boolean $view_all_def + * If the user is supposed to see his all default addresses. + * @param boolean $view_all + * If the user is supposed to see all addresses. + * @param boolean $edit_all + * If the user is supposed to edit all addresses. + * @param boolean $delete_all + * If the user is supposed to delete all addresses. + * + * @return void + */ + protected function doActionLinksTests($account, $view_own_def, $view_own, $edit_own, $delete_own, $view_all_def, $view_all, $edit_all, $delete_all) { + $this->drupalLogin($account); + + // Test view access. + $this->drupalGet('uc_addresses/action-links'); + + // Get own addresses uri's. + $own_address_default_uri = UcAddressesAddressBook::get($account->uid)->getDefaultAddress()->uri(); + $own_address_other_uri = UcAddressesAddressBook::get($account->uid)->getAddressByName($account->uid . '_other')->uri(); + + // Get addresses of other user. + $other_user_address_default_uri = UcAddressesAddressBook::get($this->accounts['extraUser']->uid)->getDefaultAddress()->uri(); + $other_user_address_other_uri = UcAddressesAddressBook::get($this->accounts['extraUser']->uid)->getAddressByName($this->accounts['extraUser']->uid . '_other')->uri(); + + // View own default addresses. + if ($view_own_def) { + $this->assertRaw($own_address_default_uri['path'], 'User can see own default address.'); + } + else { + $this->assertNoRaw($own_address_default_uri['path']); + } + // View own addresses. + if ($view_own) { + $this->assertRaw($own_address_other_uri['path'], 'User can see own addresses.'); + } + else { + $this->assertNoRaw($own_address_other_uri['path']); + } + // View all default addresses. + if ($view_all_def) { + $this->assertRaw($other_user_address_default_uri['path'], 'User can see all default addresses.'); + } + else { + $this->assertNoRaw($other_user_address_default_uri['path']); + } + // View all addresses. + if ($view_all) { + $this->assertRaw($other_user_address_other_uri['path'], 'User can see all addresses.'); + } + else { + $this->assertNoRaw($other_user_address_other_uri['path']); + } + + // Edit own addresses. + if ($edit_own) { + $this->assertRaw($own_address_other_uri['path'] . '/edit', 'User can edit own addresses.'); + } + else { + $this->assertNoRaw($own_address_other_uri['path'] . '/edit'); + } + // Edit all addresses. + if ($edit_all) { + $this->assertRaw($other_user_address_other_uri['path'] . '/edit', 'User can edit all addresses.'); + } + else { + $this->assertNoRaw($other_user_address_other_uri['path'] . '/edit'); + } + + // Delete own addresses. + if ($delete_own) { + $this->assertRaw($own_address_other_uri['path'] . '/delete', 'User can delete own addresses.'); + } + else { + $this->assertNoRaw($own_address_other_uri['path'] . '/delete'); + } + // Delete all addresses. + if ($delete_all) { + $this->assertRaw($other_user_address_other_uri['path'] . '/delete', 'User can delete all addresses.'); + } + else { + $this->assertNoRaw($other_user_address_other_uri['path'] . '/delete'); + } + } + + /** + * Creates a View for access filters test. * * @return object * The created View. @@ -214,8 +335,8 @@ class UcAddressesViewsTestCase extends UcAddressesTestCase { protected function createAccessFiltersView() { views_include('view'); - $view = new view; - $view->vid = NULL; + $view = new view(); + $view->vid = 'new'; $view->name = 'uc_addresses_access_filters'; $view->description = 'Used to test if address access filters work as expected.'; $view->tag = 'default'; @@ -279,9 +400,80 @@ class UcAddressesViewsTestCase extends UcAddressesTestCase { $view->save(); // Rebuild menus and clear caches. + views_get_all_views(TRUE); + cache_clear_all('*', 'cache_views', TRUE); + cache_clear_all('*', 'cache_views_data', TRUE); + cache_clear_all(); menu_rebuild(); - cache_clear_all('*', 'cache_views'); + + return $view; + } + + /** + * Creates a View for Action links test. + * + * @return object + * The created View. + */ + protected function createActionLinksView() { + views_include('view'); + + $view = new view(); + $view->vid = 'new'; + $view->name = 'uc_addresses_action_links'; + $view->description = 'Used to test if address action links work as expected.'; + $view->tag = 'default'; + $view->base_table = 'uc_addresses'; + $view->human_name = 'uc_addresses_action_links'; + $view->core = 6; + $view->api_version = '3.0'; + $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */ + + /* Display: Master */ + $handler = $view->new_display('default', 'Master', 'default'); + $handler->display->display_options['access']['type'] = 'none'; + $handler->display->display_options['cache']['type'] = 'none'; + $handler->display->display_options['query']['type'] = 'views_query'; + $handler->display->display_options['exposed_form']['type'] = 'basic'; + $handler->display->display_options['pager']['type'] = 'none'; + $handler->display->display_options['style_plugin'] = 'default'; + $handler->display->display_options['row_plugin'] = 'fields'; + // For Views 6.x-2.x to show all items. + $handler->override_option('items_per_page', 0); + /* Field: Ubercart Addresses: Address ID */ + $handler->display->display_options['fields']['aid']['id'] = 'aid'; + $handler->display->display_options['fields']['aid']['table'] = 'uc_addresses'; + $handler->display->display_options['fields']['aid']['field'] = 'aid'; + /* Field: Ubercart Addresses: Address name */ + $handler->display->display_options['fields']['address_name']['id'] = 'address_name'; + $handler->display->display_options['fields']['address_name']['table'] = 'uc_addresses'; + $handler->display->display_options['fields']['address_name']['field'] = 'address_name'; + /* Field: Ubercart Addresses: Link */ + $handler->display->display_options['fields']['view']['id'] = 'view'; + $handler->display->display_options['fields']['view']['table'] = 'uc_addresses'; + $handler->display->display_options['fields']['view']['field'] = 'view'; + /* Field: Ubercart Addresses: Edit link */ + $handler->display->display_options['fields']['edit']['id'] = 'edit'; + $handler->display->display_options['fields']['edit']['table'] = 'uc_addresses'; + $handler->display->display_options['fields']['edit']['field'] = 'edit'; + /* Field: Ubercart Addresses: Delete link */ + $handler->display->display_options['fields']['delete']['id'] = 'delete'; + $handler->display->display_options['fields']['delete']['table'] = 'uc_addresses'; + $handler->display->display_options['fields']['delete']['field'] = 'delete'; + + /* Display: Page */ + $handler = $view->new_display('page', 'Page', 'page_1'); + $handler->display->display_options['path'] = 'uc_addresses/action-links'; + + // Save View. + $view->save(); + + // Rebuild menus and clear caches. + views_get_all_views(TRUE); + cache_clear_all('*', 'cache_views', TRUE); + cache_clear_all('*', 'cache_views_data', TRUE); cache_clear_all(); + menu_rebuild(); return $view; } diff --git a/views/uc_addresses.views.inc b/views/uc_addresses.views.inc index 4d8f81c..2489369 100644 --- a/views/uc_addresses.views.inc +++ b/views/uc_addresses.views.inc @@ -326,6 +326,29 @@ function uc_addresses_views_data() { ), ); + // View, edit and delete links. + $data['uc_addresses']['view'] = array( + 'field' => array( + 'title' => t('Link'), + 'help' => t('Provide a simple link to the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link', + ), + ); + $data['uc_addresses']['edit'] = array( + 'field' => array( + 'title' => t('Edit link'), + 'help' => t('Provide a simple link to edit the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link_edit', + ), + ); + $data['uc_addresses']['delete'] = array( + 'field' => array( + 'title' => t('Delete link'), + 'help' => t('Provide a simple link to delete the address.'), + 'handler' => 'uc_addresses_handler_field_uc_addresses_link_delete', + ), + ); + // Make address fields available in user based views. $data['uc_addresses']['table']['join']['users'] = array( 'left_field' => 'uid', @@ -361,6 +384,15 @@ function uc_addresses_views_handlers() { 'uc_addresses_handler_filter_access' => array( 'parent' => 'views_handler_filter_boolean_operator', ), + 'uc_addresses_handler_field_uc_addresses_link' => array( + 'parent' => 'views_handler_field', + ), + 'uc_addresses_handler_field_uc_addresses_link_edit' => array( + 'parent' => 'uc_addresses_handler_field_uc_addresses_link', + ), + 'uc_addresses_handler_field_uc_addresses_link_delete' => array( + 'parent' => 'uc_addresses_handler_field_uc_addresses_link', + ), ), ); } @@ -377,4 +409,3 @@ function uc_addresses_views_post_execute($view) { } } } - diff --git a/views/uc_addresses_handler_field_uc_addresses_link.inc b/views/uc_addresses_handler_field_uc_addresses_link.inc new file mode 100644 index 0000000..487bbe0 --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link.inc @@ -0,0 +1,68 @@ +additional_fields['aid'] = 'aid'; + $this->additional_fields['uid'] = 'uid'; + } + + function option_definition() { + $options = parent::option_definition(); + $options['text'] = array('default' => '', 'translatable' => TRUE); + return $options; + } + + function options_form(&$form, &$form_state) { + parent::options_form($form, $form_state); + $form['text'] = array( + '#type' => 'textfield', + '#title' => t('Text to display'), + '#default_value' => $this->options['text'], + ); + } + + function query() { + $this->ensure_my_table(); + $this->add_additional_fields(); + } + + function render($values) { + $aid = $values->{$this->aliases['aid']}; + $uid = $values->{$this->aliases['uid']}; + $address = UcAddressesAddressBook::get($uid)->getAddressById($aid); + if ($address) { + return $this->uc_addresses_render_link($address); + } + } + + /** + * Renders the link if the user may view the address. + * + * @param UcAddressesAddress $address + * An address object. + * + * @return string + * The rendered link, if the current user has access. + */ + function uc_addresses_render_link(UcAddressesAddress $address) { + $address_user = user_load($address->getUserId()); + if (UcAddressesPermissions::canViewAddress($address_user, $address)) { + $uri = $address->uri(); + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path']; + $text = !empty($this->options['text']) ? $this->options['text'] : t('view'); + return $text; + } + } +} diff --git a/views/uc_addresses_handler_field_uc_addresses_link_delete.inc b/views/uc_addresses_handler_field_uc_addresses_link_delete.inc new file mode 100644 index 0000000..39558a6 --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link_delete.inc @@ -0,0 +1,33 @@ +getUserId()); + if (UcAddressesPermissions::canDeleteAddress($address_user, $address)) { + $uri = $address->uri(); + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path'] . '/delete'; + $text = !empty($this->options['text']) ? $this->options['text'] : t('delete'); + return $text; + } + } +} diff --git a/views/uc_addresses_handler_field_uc_addresses_link_edit.inc b/views/uc_addresses_handler_field_uc_addresses_link_edit.inc new file mode 100644 index 0000000..cb1ebae --- /dev/null +++ b/views/uc_addresses_handler_field_uc_addresses_link_edit.inc @@ -0,0 +1,33 @@ +getUserId()); + if (UcAddressesPermissions::canEditAddress($address_user, $address)) { + $uri = $address->uri(); + $this->options['alter']['make_link'] = TRUE; + $this->options['alter']['path'] = $uri['path'] . '/edit'; + $text = !empty($this->options['text']) ? $this->options['text'] : t('edit'); + return $text; + } + } +}