<commit_before><?php

/**
 * Class: RavenClientTest.
 *
 * @author  <NAME> 2017 <<EMAIL>>
 * @package phptek/sentry
 */

use SilverStripeSentry\SentryLogWriter;

/**
 * Excercises RavenClient.
 */
class RavenClientTest extends \SapphireTest
{
    
    /**
     * Setup a dummy Sentry DSN so our errors are not actually sent
     * anywhere
     */
    public function setUpOnce()
    {
        parent::setUpOnce();

        $this->usesDatabase = true;

        Config::inst()->update(
            'SilverStripeSentry\Adaptor\SentryClientAdaptor',
            'opts',
             ['dsn' => 'http://deacdf9dfedb24ccdce1b90017b39dca:deacdf9dfedb24ccdce1b90017b39dca@sentry.mydomain.nz/44']
        );
    }

    /**
     * Assert that the module's default tags make it through to the reporting process.
     *
     * @return void
     */
    public function testDefaultTagsAvailable()
    {
        $writer = SentryLogWriter::factory();
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        // Invoke SentryLogWriter::_write()
        \SS_Log::log('You have 30 seconds to reach minimum safe distance.', \SS_Log::ERR);

        $ravenSDKClient = $writer->getClient()->getSDK();
        $tagsThatWereSet = $ravenSDKClient->context->tags;

        $this->assertArrayHasKey('Request-Method', $tagsThatWereSet);
        $this->assertArrayHasKey('Request-Type', $tagsThatWereSet);
        $this->assertArrayHasKey('SAPI', $tagsThatWereSet);
        $this->assertArrayHasKey('SS-Version', $tagsThatWereSet);

        // Cleanup
        \SS_Log::remove_writer($writer);
    }

    /**
     * Assert that custom "additional" (user) data makes it through to the
     * reporting process.
     *
     * @return void
     */
    public function testdefaultUserDataAvailable()
    {
        // Setup the "fixture data" for this test
        $_SERVER['REMOTE_ADDR'] = '192.168.1.2';
        $this->logInWithPermission('admin');

        $writer = SentryLogWriter::factory();
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        $ravenSDKClient = $writer->getClient()->getSDK();
        $userDataThatWasSet = $ravenSDKClient->context->user;

        $this->assertArrayHasKey('IP-Address', $userDataThatWasSet);
        $this->assertArrayHasKey('ID', $userDataThatWasSet);
        $this->assertArrayHasKey('Email', $userDataThatWasSet);

        $this->assertEquals('192.168.1.2', $userDataThatWasSet['IP-Address']);
        $this->assertEquals(2, $userDataThatWasSet['ID']);
        $this->assertEquals('<EMAIL>', $userDataThatWasSet['Email']);

        // Cleanup
        \SS_Log::remove_writer($writer);
    }
    
    /**
     * Assert that custom "additional" (extra) data, (extras, tags env etc) makes
     * it through to the reporting process.
     *
     * @return void
     */
    public function testExtrasAvailable()
    {
        // Register SentryLogWriter with some custom context
        $fixture = [
            'extra' => [
                'foo' => 'bar'
            ],
            'env' => 'live'
        ];
        $writer = SentryLogWriter::factory($fixture);
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        // Invoke SentryLogWriter::_write()
        \SS_Log::log('You have 10 seconds to comply.', \SS_Log::ERR);

        $ravenSDKClient = $writer->getClient()->getSDK();
        $envThatWasSet = $ravenSDKClient->getEnvironment();
        $xtraThatWasSet = $ravenSDKClient->context->extra;
<commit_msg>FIX: Second stab at fixing non-DB setup in Travis env.<commit_after><?php

/**
 * Class: RavenClientTest.
 *
 * @author  <NAME> 2017 <<EMAIL>>
 * @package phptek/sentry
 */

use SilverStripeSentry\SentryLogWriter;

/**
 * Excercises RavenClient.
 */
class RavenClientTest extends \SapphireTest
{

    /**
     * In the absence of fixture files, this is needed to force SaphhireTest into
     * creating us a test DB.
     *
     * @var boolean.
     */
    protected $usesDatabase = true;

    /**
     * Setup a dummy Sentry DSN so our errors are not actually sent
     * anywhere
     */
    public function setUpOnce()
    {
        parent::setUpOnce();

        Config::inst()->update(
            'SilverStripeSentry\Adaptor\SentryClientAdaptor',
            'opts',
             ['dsn' => 'http://deacdf9dfedb24ccdce1b90017b39dca:deacdf9dfedb24ccdce1b90017b39dca@sentry.mydomain.nz/44']
        );
    }

    /**
     * Assert that the module's default tags make it through to the reporting process.
     *
     * @return void
     */
    public function testDefaultTagsAvailable()
    {
        $writer = SentryLogWriter::factory();
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        // Invoke SentryLogWriter::_write()
        \SS_Log::log('You have 30 seconds to reach minimum safe distance.', \SS_Log::ERR);

        $ravenSDKClient = $writer->getClient()->getSDK();
        $tagsThatWereSet = $ravenSDKClient->context->tags;

        $this->assertArrayHasKey('Request-Method', $tagsThatWereSet);
        $this->assertArrayHasKey('Request-Type', $tagsThatWereSet);
        $this->assertArrayHasKey('SAPI', $tagsThatWereSet);
        $this->assertArrayHasKey('SS-Version', $tagsThatWereSet);

        // Cleanup
        \SS_Log::remove_writer($writer);
    }

    /**
     * Assert that custom "additional" (user) data makes it through to the
     * reporting process.
     *
     * @return void
     */
    public function testdefaultUserDataAvailable()
    {
        // Setup the "fixture data" for this test
        $_SERVER['REMOTE_ADDR'] = '192.168.1.2';
        $this->logInWithPermission('admin');

        $writer = SentryLogWriter::factory();
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        $ravenSDKClient = $writer->getClient()->getSDK();
        $userDataThatWasSet = $ravenSDKClient->context->user;

        $this->assertArrayHasKey('IP-Address', $userDataThatWasSet);
        $this->assertArrayHasKey('ID', $userDataThatWasSet);
        $this->assertArrayHasKey('Email', $userDataThatWasSet);

        $this->assertEquals('192.168.1.2', $userDataThatWasSet['IP-Address']);
        $this->assertEquals(1, $userDataThatWasSet['ID']);
        $this->assertEquals('<EMAIL>', $userDataThatWasSet['Email']);

        // Cleanup
        \SS_Log::remove_writer($writer);
    }
    
    /**
     * Assert that custom "additional" (extra) data, (extras, tags env etc) makes
     * it through to the reporting process.
     *
     * @return void
     */
    public function testExtrasAvailable()
    {
        // Register SentryLogWriter with some custom context
        $fixture = [
            'extra' => [
                'foo' => 'bar'
            ],
            'env' => 'live'
        ];
        $writer = SentryLogWriter::factory($fixture);
        \SS_Log::add_writer($writer, \SS_Log::ERR, '<=');

        // Invoke SentryLogWriter::_write()
        \SS_Log::log('You have 10 seconds to comply.', \SS_Log::ERR);

        $ravenSDKClient = $writer->getClient()->getSDK();
        $envThatWasSet = $ravenSDKClient->getEnvironment();
        $xtraThatWasSet = $ravenSDKClient->context->extra;

{'fraction_non_alphanumeric': 0.14565431750950839, 'fraction_numerical': 0.013804761233976616, 'mean_word_length': 2.620601733809281, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 6, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '741937', 'n_tokens_mistral': 2295, 'n_tokens_neox': 2162, 'n_words': 414}