Exemplo n.º 1
0
 public static function handle_webpush_register()
 {
     // TODO: Enable nonce verification.
     // check_ajax_referer('register_nonce');
     WebPush_DB::add_subscription($_POST['endpoint'], $_POST['key']);
     wp_die();
 }
Exemplo n.º 2
0
 function test_add_get_remove()
 {
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     WebPush_DB::add_subscription('http://localhost/1', 'aKey1', '89IdFKBhvi9H5LlvawK9Iw==');
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     WebPush_DB::add_subscription('http://localhost/2', 'aKey2', '89IdFKBhvi9H5LlvawK9Iw==');
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(2, count($subscriptions));
     $this->assertEquals('http://localhost/1', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey1', $subscriptions[0]->userKey);
     $this->assertEquals('http://localhost/2', $subscriptions[1]->endpoint);
     $this->assertEquals('aKey2', $subscriptions[1]->userKey);
     WebPush_DB::remove_subscription('http://localhost/1');
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertTrue(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost/2', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey2', $subscriptions[0]->userKey);
     WebPush_DB::remove_subscription('http://localhost/2');
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/1'));
     $this->assertFalse(WebPush_DB::is_subscription('http://localhost/2'));
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(0, count($subscriptions));
 }
Exemplo n.º 3
0
 public static function handle_register()
 {
     if (isset($_POST['oldEndpoint']) && $_POST['oldEndpoint'] !== $_POST['endpoint']) {
         WebPush_DB::remove_subscription($_POST['oldEndpoint']);
     }
     WebPush_DB::add_subscription($_POST['endpoint'], isset($_POST['key']) ? $_POST['key'] : '', isset($_POST['auth']) ? Base64Url::decode($_POST['auth']) : '');
     if (isset($_POST['newRegistration'])) {
         update_option('webpush_accepted_prompt_count', get_option('webpush_accepted_prompt_count') + 1);
     }
     wp_die();
 }
 function test_unregister_priv()
 {
     WebPush_DB::add_subscription('http://localhost', 'aKey', '89IdFKBhvi9H5LlvawK9Iw==');
     $_POST['endpoint'] = 'http://localhost';
     try {
         $this->_handleAjax('webpush_unregister');
         $this->assertTrue(false);
     } catch (WPAjaxDieStopException $e) {
         $this->assertTrue(true);
     }
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(0, count($subscriptions));
 }
Exemplo n.º 5
0
 function test_reregistration_with_removal()
 {
     $_POST['endpoint'] = 'http://localhost/1';
     $_POST['key'] = 'aKey';
     $_POST['auth'] = '89IdFKBhvi9H5LlvawK9Iw==';
     $_POST['newRegistration'] = 'true';
     $_POST['oldEndpoint'] = 'http://localhost/2';
     WebPush_DB::add_subscription('http://localhost/2', '', '');
     try {
         $this->_handleAjax('nopriv_webpush_register');
         $this->assertTrue(false);
     } catch (WPAjaxDieStopException $e) {
         $this->assertTrue(true);
     }
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost/1', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey', $subscriptions[0]->userKey);
     $this->assertEquals('89IdFKBhvi9H5LlvawK9Iw', Base64Url::encode($subscriptions[0]->userAuth));
     $this->assertEquals(1, get_option('webpush_accepted_prompt_count'));
 }
 function test_success_remove_invalid_subscription()
 {
     $oldNum = getSentNotificationNum();
     WebPush_DB::add_subscription('http://localhost:55555/400', 'aKey2', '89IdFKBhvi9H5LlvawK9Iw==');
     $post = get_post($this->factory->post->create(array('post_title' => 'Test Post Title')));
     $main = new WebPush_Main();
     $main->on_transition_post_status('publish', 'draft', $post);
     $payload = get_option('webpush_payload');
     $this->assertEquals('Test Blog', $payload['title']);
     $this->assertEquals('Test Post Title', $payload['body']);
     $this->assertEquals('', $payload['icon']);
     $this->assertEquals('http://example.org/?p=' . $post->ID, $payload['url']);
     $this->assertEquals($post->ID, $payload['postID']);
     $this->assertEquals(0, get_post_meta($post->ID, '_notifications_clicked', true));
     $this->assertEquals(1, get_post_meta($post->ID, '_notifications_sent', true));
     $this->assertEquals($oldNum + 2, getSentNotificationNum());
     // Test that the invalid subscription gets removed.
     $subscriptions = WebPush_DB::get_subscriptions();
     $this->assertEquals(1, count($subscriptions));
     $this->assertEquals('http://localhost:55555/201', $subscriptions[0]->endpoint);
     $this->assertEquals('aKey', $subscriptions[0]->userKey);
 }
Exemplo n.º 7
0
 public static function handle_webpush_register()
 {
     WebPush_DB::add_subscription($_POST['endpoint'], $_POST['key']);
     wp_die();
 }