/**
  * Tests the Stripe Subscription gateway
  * @test
  */
 function stripeplan_subscription()
 {
     $gateway = MS_Model_Gateway::factory(MS_Gateway_Stripeplan::ID);
     $user_id = TData::id('user', 'editor');
     $membership_id = TData::id('membership', 'recurring');
     $subscription = TData::subscribe($user_id, $membership_id);
     $controller = MS_Factory::load('MS_Controller_Gateway');
     $gateway->update_stripe_data();
     $data = array('card' => array('number' => '4242424242424242', 'exp_month' => 12, 'exp_year' => date('Y') + 1, 'cvc' => '314'));
     $res = M2_Stripe_Token::create($data);
     $token = $res->id;
     $form_data = array('_wpnonce' => wp_create_nonce($gateway->id . '_' . $subscription->id), 'gateway' => $gateway->id, 'ms_relationship_id' => $subscription->id, 'step' => 'process_purchase', 'stripeToken' => $token, 'stripeTokenType' => 'card', 'stripeEmail' => '*****@*****.**');
     $_POST = $form_data;
     $_REQUEST = $_POST;
     // Right now the subscription must have status PENDING
     $this->assertEquals(MS_Model_Relationship::STATUS_PENDING, $subscription->status);
     /*
      * This function processes the purchase and will set the subscription
      * to active.
      */
     $controller->process_purchase();
     // Check the subscription status.
     $this->assertEquals(MS_Model_Relationship::STATUS_ACTIVE, $subscription->status);
     $this->assertEquals(1, count($subscription->payments));
     // Modify the expiration date to trigger another payment.
     $today = date('Y-m-d');
     $subscription->expire_date = $today;
     $this->assertEquals($today, $subscription->expire_date);
     $this->assertEquals(0, $subscription->get_remaining_period());
     // Trigger next payment and validate it.
     $subscription->check_membership_status();
     $this->assertEquals(2, count($subscription->payments));
     // Modify the expiration date to trigger another payment.
     $subscription->expire_date = $today;
     $this->assertEquals($today, $subscription->expire_date);
     $this->assertEquals(0, $subscription->get_remaining_period());
     // Trigger next payment and validate it.
     // THIS TIME NO PAYMENT SHOULD BE MADE because paycycle_repetitions = 2!
     $subscription->check_membership_status();
     $this->assertEquals(2, count($subscription->payments));
     // Also the subscription should be cancelled at stripe now.
     $customer_id = $subscription->get_member()->get_gateway_profile(MS_Gateway_Stripe_Api::ID, 'customer_id');
     $customer = M2_Stripe_Customer::retrieve($customer_id);
     $invoice = $subscription->get_previous_invoice();
     $stripe_sub_id = $invoice->external_id;
     $stripe_sub = $customer->subscriptions->retrieve($stripe_sub_id);
     $this->assertEquals('active', $stripe_sub->status);
     $this->assertTrue($stripe_sub->cancel_at_period_end);
     // Clean up.
     $customer->delete();
 }
 /**
  * Test changing the membership payment plan from permanent to recurring.
  * @test
  */
 function permanent_to_recurring()
 {
     $user_id = TData::id('user', 'editor');
     $membership_id = TData::id('membership', 'simple');
     $subscription = TData::subscribe($user_id, $membership_id);
     $invoice = $subscription->get_current_invoice();
     $invoice->pay_it('stripe', 'external_123');
     // Now we have a user that is subscribed to a permanent membership.
     $start_date = MS_Helper_Period::current_date();
     $this->assertEquals($start_date, $subscription->start_date);
     $this->assertEquals('', $subscription->expire_date);
     $this->assertTrue($invoice->is_paid());
     $this->assertEquals('active', $subscription->status);
     // This check should not modify the subscription.
     $subscription->check_membership_status();
     $this->assertEquals($start_date, $subscription->start_date);
     $this->assertEquals('', $subscription->expire_date);
     $this->assertTrue($invoice->is_paid());
     $this->assertEquals('active', $subscription->status);
     // Now the user changes the membership to recurring.
     $membership = $subscription->get_membership();
     $membership->payment_type = MS_Model_Membership::PAYMENT_TYPE_RECURRING;
     $membership->pay_cycle_period_unit = 7;
     $membership->pay_cycle_period_type = 'days';
     $membership->save();
     $this->assertEquals(MS_Model_Membership::PAYMENT_TYPE_RECURRING, $membership->payment_type);
     // The membership status check is automaticaly done every six hours.
     // It will update the subscription details to match the new payment type.
     $subscription->check_membership_status();
     // Confirm that the existing subscription has a correct expire date.
     $expire_date = MS_Helper_Period::add_interval(7, 'days', $start_date);
     $this->assertEquals($expire_date, $subscription->expire_date);
 }